Please I need help :/
How can I copy values from a ArrayList to another.
if I have a ArrayList containing 10 values and I want to copy the 2nd 4rd values and so on.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Please I need help :/
How can I copy values from a ArrayList to another.
if I have a ArrayList containing 10 values and I want to copy the 2nd 4rd values and so on.
Review the ArrayList API for methods that should be helpful, then post what you've tried when you need help.
How can I copy values from a ArrayList to another. if I have a ArrayList containing 10 values and I want to copy the 2nd 4rd values and so on.
Here's my code:
ArrayList tab = new ArrayList ();
ArrayList tab2 = new ArrayList ();
{
int i=1;
int j=0;
while ( i < tab.size () )
{
tab.get(i);
i=i+2;
tab2.add(i);
j=j+1;
}
}
Ok, first of all, your loop doesn't make sense.
Tab doesn't contain any object so it has a size of 0, but you say while (1 < 0) do something? The code between the brackets will never be reached.while ( i < tab.size () )
Just so you know:
is the same asi = i + 2
i += 2
The first element of an array has an index of 0
This means the second element has an index of 1, the forth element has an index of 3, etc
So you'll probably want to give your i a start value of 0 instead of 1
What do you use that j variable for? You never use it?
How I would do it:
The upper block puts 10 integer values in the tab arraylist, ranging from 0 to 9ArrayList<Integer> tab = new ArrayList<>(); for (int i = 0; i < 10; i++) tab.add(i); ArrayList<Integer> tab2 = new ArrayList<>(); for (int i = 0; i < 10; i += 2) tab2.add(tab.get(i + 1));
The other block then adds the values of tab at index 1, 3, 5, 7 and 9 to tab2, these are respectively the second, forth, sixth, eight and tenth integer in the tab arraylist
e93 (January 2nd, 2014)
Duplicate topic. This one has more effort.
Please read this topic to learn how to post code correctly and other useful topics for newcomers.
in the second arraylist shouldn't it be get(i) not i+1 ?
and for int i =1?
The variable i will respectively go trough the following values:for (int i = 0; i < 10; i += 2)
0, 2, 4, 6, 8
i + 1 then gives these values:tab2.add(tab.get(i + 1));
1, 3, 5, 7, 9
And this are the indexes that are required for what you want to do (:
I'm also still new to Java and my teacher told me that you should try to always give your integer variable in such a for-next loop a value of zero. Good programming practice I guess!
e93 (January 3rd, 2014)