• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting the index number of an arraylist element.  RSS feed

 
Joshua Jones
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Let me explain my code, and what I'm having trouble with,

ok so my code here searches through an arrayList and populates some fields with the information held in an element, now I have corresponding arrays that contain other data in the same element position in their respective arrays, so the index for this array List's useful information is the same as the index of the other Arrays, now I can display my searched results element completely fine, but I want to produce a similar result for 2 more arrays without having to search either one of them, by using the array index number, I would like to know of any ideas of how i can return the index number of the current element that satisfies the if statement.
 
Joshua Jones
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BUMP
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a counter before you enter your loop. Increment it each pass through the loop. Use that as the index into the other array.

Or, if you're using Lists rather than arrays, just iterate both Lists at the same time.

However, if you're using a "parallel array" (or "parallel list") approach, that's usually a sign of a design error. A better approach would probably be to define a class that contains one "row" across corresponding elements of all of your arrays.
 
Joshua Jones
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Create a counter before you enter your loop. Increment it each pass through the loop. Use that as the index into the other array.

Or, if you're using Lists rather than arrays, just iterate both Lists at the same time.

However, if you're using a "parallel array" (or "parallel list") approach, that's usually a sign of a design error. A better approach would probably be to define a class that contains one "row" across corresponding elements of all of your arrays.



how would I create such a counter, I have tried but I just end up printing the value of how many passes the loop makes overall not how many passes till the if statement is met?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joshua Jones wrote:BUMP
Read this.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joshua Jones wrote:
Jeff Verdegan wrote:Create a counter before you enter your loop. Increment it each pass through the loop. Use that as the index into the other array.

Or, if you're using Lists rather than arrays, just iterate both Lists at the same time.

However, if you're using a "parallel array" (or "parallel list") approach, that's usually a sign of a design error. A better approach would probably be to define a class that contains one "row" across corresponding elements of all of your arrays.



how would I create such a counter, I have tried but I just end up printing the value of how many passes the loop makes overall not how many passes till the if statement is met?


So stop counting once your if condition is met. The simplest way to do that is just to end the loop, either using break or by setting a flag that you test for in the condition. If you still need to process the rest of the loop even after you meet the if condition, then set a flag, and only increment that count if the flag has not been set.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!