• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java variable storage  RSS feed

 
Greenhorn
Posts: 16
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello cowboys,

I will now proceed to ask a question that I belive I know, but would like some reassurance from others who are more experinced than I am. My questions is: If I create two ArrayLists, one which holds an arbitrary object, and another which holds an ArrayList, then I proceed to add arbitrary objects to the ArrayList that holds arbitrary objects & create multiple of these ArrayLists, then proceeded to add these newly created ArrayLists to an ArrayList that holds ArrayLists, then I clear() (cleared out) one of the ArrayLists that held Arbitrary objects. Would the ArrayList holding ArrayLists be affected by this, that is, that the ArrayList holding arbitrary objects that was cleared out would be visible inside that ArrayList that held this ArrayList? I have some code to illustrate my question . . . . . I know the answer is yes, but I wanted to know others opinions. What would be a way to add the elements one ArrayLists as sets of Arrays to an ArrayList that holds ArrayLists without the ArrayList that holds ArrayLists being tightly coupled with the ArrayList of arbitrary objects.

ArrayListPractice.java (Standalone Class)


 
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Avila wrote:What would be a way to add the elements one ArrayLists as sets of Arrays to an ArrayList that holds ArrayLists without the ArrayList that holds ArrayLists being tightly coupled with the ArrayList of arbitrary objects.

One way would be to use the constructor of ArrayList which takes Collection as argument.

Can you come up with other ways and post here.
 
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP: Yes,you are correct!
first of all change the reference type to List(program to interface).
ArrayList is a mutable object and is not thread safe.actually the list inside the ArrayList(List of List) is pointing to the same that has been added to it so a change had a effect in both variables(orignal reference and the reference inside the List(holding set of list)).
Okay for solving this problem you can make use of a copy(actually deep copy) of arraylist that you want to add to the other list.by deep copy i mean the list can have further reference to mutable object and those mutable reference can have further mutable objects and so on..that's a basic approach.
however,their is a List called CopyOnWriteArrayList defined in the java library to solve these type of problems you can use it.it's thread safe and is a part of the Concurrent Collections.please hit the link to go through the API.

Hope it helps!!

Kind regards,
Praveen
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:
One way would be to use the constructor of ArrayList which takes Collection as argument.


And what if the stringList has further mutable references ?
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:And what if the stringList has further mutable references ?

Yeah I understand this is not a generic solution.
And thanks for information about CopyOnWriteArrayList.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Chris and Joney: And, Welcome to CodeRanch!
 
Chris Avila
Greenhorn
Posts: 16
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! Awesome! Thanks so much for the help @praveen kumaar & @ Tapas Chand! I appreciate it very much.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!