Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please post code, so that we can reply you.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can please any one explain me this line: in the above code

for (String s : arr1)
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you ..
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic