Forums Register Login

Collections

+Pie Number of slices to send: Send
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 ".";
+Pie Number of slices to send: Send

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

PS: use code tag for code.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks i got it
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 817 times.
Similar Threads
Problem in Arrays passing.
Array.length
here is a question and i have a very similar code but i need to convert it
exceptions and type casting confusion
Array Problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:33:22.