• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList and for loop  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've spent days trying to figure this out and unfortunately been getting nowhere...


I want to be able to add more than one element to an ArrayList for every loop, but on the next iteration the elements are different.
For example...
and the output to be:
1,2
3,4
5,6

Any ideas would be very helpful. I tried to change the loop index but that also didn't work.

 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please think carefully about what you are planning to do. You look as if you are adding six numbers. Is there any difference between the types of the numbers? Then you seem to want to print them out in pairs. Are you adding numbers or pairs of numbers? Remember two numbers do not constitute a pair. Are you adding six things or three?

Start by going through that loop, without the List, and get it to print
1,2
3,4
5,6

I tried to change the loop index but that also didn't work.
That looks as if you are guessing. Don't guess. Or do guess. You can take a million guesses and as long as they are different, there is a good chance you will get a correct solution. Somewhere…

Or you can think about it and work out what you want to do and get it to work first time.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, maybe the advice I gave you earlier was mistaken. Maybe it would be better to start by filling a List so you can get this outputForget all about pairs for the time being. I think you will need a different loop. Remember what the basic form of a for loop is.
 
Alice Greg
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you - I managed to print out pairs like you initially said by writing this code:



However I want to be able to print separate arrays eventually of different length so will require a nested loop so the outcome will be for example:

list1: 1,2 list2: 1
list1: 3,4 list2: 2


however I can't manage to combine the two loops without one either printing in pairs whilst the other just repeats the same number until it's reached the size of the other loop.

Do you advise using a for-each loop instead?

 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good start. I don't think you need the two ints beforehand. It is all right simply to write i or i + 1.
Now you have a loop which will actually go up in pairs, you want to add those numbers to your List and print in to get the output
[1, 2, 3, 4, 5, 6]
as I said yesterday.
By the way: What is list in your program? Is it an array? It won't compile otherwise because only arrays have .length. It is potentially confusing to call an array list when you have a List as well.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Greg wrote: . . .
Do you advise using a for-each loop instead?
That sounds like you guessing again. Since a for‑each loop treats its subject as read‑only, it is highly unlikely that a for‑each loop will work. Unless you have an array [1, 2, 3, 4, 5, 6] in which case you can use the array to fill the List. But there is a much quicker way to fill a List from an array.
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:But there is a much quicker way to fill a List from an array.

I believe this method is more suitable.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably is, yes, Paweł.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!