File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections" Watch "Collections" New topic
Author

Collections

Suresh KumarPandey
Ranch Hand

Joined: Sep 08, 2012
Posts: 46
String[] arr = { "Java", "Champ", "." };
List<String> list = (List<String>) Arrays.asList(arr);
arr[2] = ".com"; // line 210.
for (String word : list) {
System.out.print(word);
}
Can any one explain why it is printing JavaChamp.Com ,why not JavaChamp. because arr[2] is being created when arr[2]was ".";
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72


because of the above mentioned line, if you remove that you will get the result that you expect.

PS: use code tag for code.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Suresh KumarPandey wrote:why not JavaChamp. because arr[2] is being created when arr[2]was ".";

You are replacing 2nd index value(".") by .com . hence result . the key point is here you are not changing the size of the array/list
Danjel Nyberg
Greenhorn

Joined: Sep 12, 2012
Posts: 15

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess.

/Danjel
Suresh KumarPandey
Ranch Hand

Joined: Sep 08, 2012
Posts: 46
hii thanks for the reply
what i am not getting is when we are creating the list array is having "."
after creating the list why changing arr[2] changes the answer
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Suresh KumarPandey wrote:hii thanks for the reply
what i am not getting is when we are creating the list array is having "."
after creating the list why changing arr[2] changes the answer


Daniel already quoted the reason directly from the Javadoc .... what is it about the response that you don't understand???

http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#asList%28T...%29

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 773
Hi Suresh KumarPandey buddy,
Read Kathy's study guide about TreeMap. There is a section about backed collection.
Backed collection means when a collection change, its backed collection changes also.

To me, it seems like list is a backed storage of arr. So, any change in arr will affect list or vice-versa. You can try to change any string in list to see if the change is shown in arr.
Try to do this:


I am sure it will output a runtime exception. Because arr is defined as size 3 at the beginning. You tried to add one more string to the list, but arr cannot increase its size.
Suresh KumarPandey
Ranch Hand

Joined: Sep 08, 2012
Posts: 46
Thanks i got it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections