This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft 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 Java 8 in Action this week in the Java 8 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't get array-backed List to "write through" back to the array
 
Similar Threads
resize two arrays ... guide me ...
Comparing ArrayList and double array[]
Find min and max in ArrayList
Three Dimensional ArrayList?
Cast Object to Object[]