This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

forEach-loop starting at an index  RSS feed

 
Rancher
Posts: 502
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi fellow Ranchers

I don't know if this is a question for the beginners or the java general forum, since we only covered collections in the second module of our course (java Intermediate), but after coding for a while, I myself would count lists still as beginning Java.
Anyhow... I would like to know if their a simpler/cleaner way of having a for each that starts at an index and when it reaches the end of the list starts back at te beginning until it reaches the startingIndex.

for now I'm using something like:


I guess I could change the for condition to
But it's the if statement I'm looking to get rid of
 
Marshal
Posts: 61715
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That isn't a for‑each loop, but an ordinary for loop.
Do you want to iterate the collection repeatedly? Let's have an array which we shall iterate 10×:-The %8d tag will cause ten numbers to be printed across the width of the command line if it has its default width of 80, so you should get twelve lines.
 
Campbell Ritchie
Marshal
Posts: 61715
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to start from an index, print the second half of the collection then the first half, try this sort of thing:-I trust you know whether you start at index 6 or 7.
You can't do that with a for‑each loop. You can probably rewrite that do loop as an ordinary for loop.
 
Daniel Demesmaecker
Rancher
Posts: 502
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I should tell you what I'm trying to do, to make it more clear.
I'm programming the bussiness logic of a poker game, no gui, pure console, I know in real life it would be useless, but it's just an exercise in the logic.
This particular piece of code has to descide which player's turn it is to eather call, raise or fold, the startIndex, is the player who has the bigBlind, so it should start at the next player go around and end with the bigBlind

That isn't a for‑each loop, but an ordinary for loop.


I realized that, that's why I added the
 
Campbell Ritchie
Marshal
Posts: 61715
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:. . . poker game . . .

Sounds similar to my second example.

that's why I added the

Afraid that won't work like that. A for‑each group iterates the entire array/collection from start to finish. You need a different loop if you need to start in the middle.
 
Daniel Demesmaecker
Rancher
Posts: 502
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Afraid that won't work like that. A for‑each group iterates the entire array/collection from start to finish. You need a different loop if you need to start in the middle.


When using the forEach I wouldn't do anything with the players, I would simply use it to make sure I loopted the correct number of times.
the player I'm getting by the startIndex
 
Daniel Demesmaecker
Rancher
Posts: 502
11
Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Afraid that won't work like that. A for‑each group iterates the entire array/collection from start to finish. You need a different loop if you need to start in the middle.


When using the forEach I wouldn't do anything with the players, I would simply use it to make sure I loopted the correct number of times.
the player I'm getting by the startIndex
 
Campbell Ritchie
Marshal
Posts: 61715
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:. . . I would simply use it to make sure I loopted the correct number of times.
the player I'm getting by the startIndex

Since the index is hidden from the programmer by a for‑each loop, you are going to have some very awkward code like that.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to call toArray(), can't you just call size ()?

I might be wrong but the two times you do call this is it not creating a new array copying your arraylist and then you check the size and do nothing with the array?

I am not entirely sure what it is you're trying to do but you can loop a range like this.

IntStream.range(start, end).forEach(
i -> {
       // some player stuff
});

I typed this on my phone so apologies if syntax isn't quite right!
 
Campbell Ritchie
Marshal
Posts: 61715
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that end is usually less than start.
 
Ranch Hand
Posts: 106
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using a LinkedList as a Deque in my Monopoly and "move" it to the right index - and then just using foreach.

A LinkedList allows a simple implementation of a Stack you can think of as a wheel wich you turn to change the index but otherwise keep the order - and you can turn it in both ways.
 
Campbell Ritchie
Marshal
Posts: 61715
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you thinking of a singly linked list or a doubly linked list, and does it have null on its tail node, or does the tail node point back to the head node?
 
We find this kind of rampant individuality very disturbing. But not this tiny ad:
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!