• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't find appropriate COPY method for LIST  RSS feed

 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have what I hope will be a simple question but I can't seem to get my mind around how to accomplish what I need. I cannot find a COPY like method.

Objective: I have a list of type NonStockRequisition requisitions where by I need to COPY this list for each user that I have in another list and then update an element within an arrayList that is within the requisitions object with the user from the list that I am looping through.

Here is the code that I tried to conjour up in my main method but to no avail:



Any help or direction would be greatly appreciated.

Regards.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To copy a list, you can construct a new list passing the old one as a constructor parameter:

List<String> copy = new ArrayList<String>(users);

Be careful, though, that you remember that Java collections hold references to objects rather than the objects themselves. After the above code, "copy" and "users" are two separate lists, but they both refer to the same objects, and if you change a property of one of those objects, holders of both "users" and "copy" will see the change. If you add or remove objects from one of the lists, however, only the list you actually change will reflect this; the other list is unaffected.
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,

Thanks so much for your help. It is appreciated.

This is probably a stupid question, but how would I have known to go to ArrayList to look for copy functionality as a beginner? I saw a CopyOnWriteArrayList class but I guess I just did not understand how to get there to here.

Thanks again for our reply and your time.

Regards.
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It takes some experience to be able to predict where you may find a method you can use.
First thing is to think of what would be a logical place.
In thise case classes implementing List are such a place, so you go and look there. Of those ArrayList is the most used one as it offers the best overall performance (another thing experience will teach you).

There are of course also books written full of every detail of every part of the language and its libraries for you to indulge in
 
Melinda Savoy
Ranch Hand
Posts: 387
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeroen,

Thanks for the reply. Your help was very much appreciated as I thought I was going crazy trying to find an answer to this question before Ernest was kind enough to give me some direction.

Thanks again for each reply.

Regards.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!