File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections

 
Suresh KumarPandey
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
Eclipse IDE Java MS IE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Himai Minh
Ranch Hand
Posts: 1142
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks i got it
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic