• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B doubt pg 559 toArray(),asList()

 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have a doubt in Converting array to lists to arrays .
ON page 559,After a program code, Book says ...
when we convert from array to list(asList),later if we change the array,changes are also reflected in list.
This thing is working..but when i tried opposite ..changes done in list ,not showing in converted array with toArray()

I cant understand why aslist() shows modificatinos ,wht toArray() cannot

thankyou
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please post code, so that we can reply you.
 
Neha Bhattad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lucky, only the asList() method claims to bind the array and the newly created list.
toArray() does not bind the list and array.
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my code...
Its from K&B only. with few changes ...made.

<code>
import java.util.*;

class TestSort11 {
public static void main(String args[]) {
String[] arr = { "this ", "is", "a", "java", "program" };

List list = Arrays.asList(arr); //Converted to list

System.out.println(" " + list);//otput

arr[0] = "THIS";//ade changes in array
System.out.println(" " + list);//changes reflectd in list

String arr1[]=(String[])list.toArray();//from list to array

for (String s : arr1)
System.out.print(s + " ");//array display
list.set(0, "this");//changes
System.out.println("after change " + list);//changes done in array not showing in list
for (String s : arr1)
System.out.print(s + " ");

}
}
</code>
 
anamika henry
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can please any one explain me this line: in the above code

for (String s : arr1)
 
anamika henry
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can please any one explain me this line: in the above code

for (String s : arr1)
 
Abdullah Mamun
Ranch Hand
Posts: 99
Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by anamika henry:
can please any one explain me this line: in the above code

for (String s : arr1)


This is known as for-each loop and introduced in Java 1.5
What it does is actually traversing a collection or array.
In this example, arr1[] is a String array and this line is looping through each element of this array.
 
anamika henry
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you ..
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lucky J Verma:
I cant understand why aslist() shows modificatinos ,wht toArray() cannot
asList() returns a wrapper object for the array which uses the array internally. In contrast, toArray() always creates a new array.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic