• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to copy the every element in first list to second list  RSS feed

 
nalamati satyanarayana
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everbody,

i have one question how to add all elements in first list to second list like
List firstList = new ArrayList();
List secondList = new ArrayList();
please help me
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do it in several ways.

1. You can loop through list_1, and for each element
- remove value, x, from list_1
- insert x into list_2

The API for an ArrayList will show you how to insert/remove/get elements
from it.

2. ArrayLists have an method that adds all the elements from one collection
to the array list.

The API for the arraylist can be found here: http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html

/Svend Rost
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
secondList.addAll(firstList)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!