Click to See Complete Forum and Search --> : Adding elements to a list


Rabbit3cat
02-25-2006, 12:10 PM
I have two lists, one that contains a number of strings and one that is empty. I want to add one of every single element in the first list onto the second list, and where an element occurs more than one time in the first list i want to add it a second time onto the second list.
So that one of every string will be on the second list, and two of every string that appears more than one time on the first list will appear on the second list.
Just to make that more clear, if list 1 contains
[it, is, a, cold, morning, it, it, one, a]
then the second list would contain
[it, is, a, cold, morning, it, one, a]
the third 'it' will not be added

I have a method to add one of every element to the second list:

Iterator iterW = W.iterator();
while ( iterW.hasNext()){

Object e = iterW.next();

if(!check.contains(e)) {

check.add(e);
}
}


I dont know how to add all duplicates just one more time, can anybody help me with this problem?
Thanks

Khalid Ali
02-25-2006, 01:32 PM
how about this

if(!check.contains(e)) {
check.add(e);
check.add(e);
}

this means whenever there is a match that value is being added twice, to add a clist completely to another one you can use addAll() method

Rabbit3cat
02-25-2006, 02:23 PM
Thanks, although i only want to add a value twice to the second list, when it is in the first list more than one time