Here's my problem: Let's say that I have an Object array of long length, in my case it's class Player. I set up some loop to traverse through Player[] and want to find all Player objects that meet my requirements. However, the amount of result is unknown before execution and I need to assign each of the candidate elements a unique name. How can I change/iterate those variable names being assigned while looping?
I recently just started learning about Java. Based on what I know so far the only solution to keep tracking them might be ArrayList or List?
Can someone help me find proper solutions?
Here I simplified my question in a scenario to expect only 2 candidate elements.
Player[] players; //this is a sorted array of players
int candidateNum = 0; //this is for keeping track of the amount
Player A;
Player B;
int index = players.length - 1;
...
while(candidateNum < 2) {
if (players[index].getLevel() > 10) {
Player A??? //what am I gonna do to name Player A?
//and how should I name Player B which is coming out within next several loops?
candidate++;
}
index--;
}
//furthermore, what about in a case that results' amount is unknown?