This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Can't get array-backed List to Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can Watch "Can New topic
Author

Can't get array-backed List to "write through" back to the array

Joe Kaz
Greenhorn

Joined: Aug 13, 2001
Posts: 14
Greetings,
I can't seem to get updates to an array-backed List, created with asList(), to write back to the originating array.
The Arrays class asList() API says: "Changes to the returned List "write through" to the array."
This code seems to demonstrate that the array is not being updated. Can anyone tell me what I'm doing wrong?
Thanks much
Joe Kaz
import java.lang.reflect.Array;
import java.util.*;
public class TestArray {
public static void main(String args[]) {
String[ ] array = {"You", "Me", "Her", "Him"};
ArrayList ourList;
ourList = new ArrayList (Arrays.asList(array) );
System.out.println("List length: " + ourList.size());
System.out.println("List contains: " + ourList);
System.out.println("Array length: " + array.length);
for (int i =0; i<Array.getLength(array); i++) System.out.println("Array is: " + Array.get(array, i));
System.out.println(" ");
ourList.set( 1, "XXXXX"); // change AN OBJECT in THE LIST
ourList.add("YYYYY"); // ADD AN OBJECT TO THE LIST
System.out.println("List length now: " + ourList.size());
System.out.println("List now contains: " + ourList);
System.out.println("Array length now: " + array.length);
for (int i =0; i<Array.getLength(array); i++) System.out.println("Array is now: " + Array.get(array, i));
} }
Jon Strayer
Ranch Hand

Joined: Dec 04, 2002
Posts: 133
Originally posted by Joe Kaz:
Greetings,
I can't seem to get updates to an array-backed List, created with asList(), to write back to the originating array.
The Arrays class asList() API says: "Changes to the returned List "write through" to the array."

True, but the constructor for ArrayList copies the data from the passed in collection.


Jon
 
jQuery in Action, 2nd edition
 
subject: Can't get array-backed List to "write through" back to the array
 
Similar Threads
Cast Object to Object[]
Three Dimensional ArrayList?
Find min and max in ArrayList
Comparing ArrayList and double array[]
resize two arrays ... guide me ...