wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B doubt pg 559 toArray(),asList() 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 "K&B doubt pg 559 toArray(),asList()" Watch "K&B doubt pg 559 toArray(),asList()" New topic
Author

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

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
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

Joined: Jun 18, 2007
Posts: 383
Can you please post code, so that we can reply you.


SCJP-1.5<br />SCWCD-1.4
Neha Bhattad
Ranch Hand

Joined: Aug 20, 2007
Posts: 30
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

Joined: Apr 11, 2007
Posts: 278
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

Joined: Mar 09, 2007
Posts: 30
can please any one explain me this line: in the above code

for (String s : arr1)
anamika henry
Ranch Hand

Joined: Mar 09, 2007
Posts: 30
can please any one explain me this line: in the above code

for (String s : arr1)
Abdullah Mamun
Ranch Hand

Joined: Mar 19, 2007
Posts: 99

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.


MooN
anamika henry
Ranch Hand

Joined: Mar 09, 2007
Posts: 30
thank you ..
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B doubt pg 559 toArray(),asList()