• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How it is doing?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test {
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 //1

arr[0] = "THIS"; //add changes in array //2
System.out.println(" " + list);//changes reflectd in list //3
}
}
In the class above, the line //1 prints [this , is, a, java, program]
after that i am changing the arr[0] element in the String Array in line //2.
But line three prints, [THIS, is, a, java, program]
I am wondering how it is printing the modified value?; i am changing the 0th value in the String Array only not in the list; also I am not calling Arrays.asList(arr); after the line //2. But still it prints the changes made.
Can any one explain this flow
??
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you refer the API, then you will find something like:

"asList() - Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)"

That's the reason the changes you make to the List shall be propagated back to the array as well.

-Kamal
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Rajava" - Please check your private messages. You can see them by clicking on My Profile.
 
Raja Samy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Thanks Kamol.
I will chk it up.
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic