-1

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? 
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Lam598
  • 1
  • 2
  • 2
    Variable names have to be known at compile time. If you need to link data to a key, use a `Map` instead – knittl Jan 29 '23 at 12:28
  • You already are using the answer - arrays. Your snippet is unclear; is `players` larger than 2 - this code selects 2 players out of the many players in `players`? – rzwitserloot Jan 29 '23 at 12:33
  • Why are you using an array? Use a collection (eg a List) in preference to an array. Collections are so much easier to work with. I only use arrays on exceedingly rare occasions - call it "never". – Bohemian Jan 29 '23 at 12:37
  • @Bohemian cause that's an exercise from a textbook I read lol... The question was assigned using `Players` array. And I was supposed to find several top level `players`. – Lam598 Jan 29 '23 at 12:59
  • @rzwitserloot `players` is a rather long array. In the snippet I am looking for a way to find only the last two players in `players` that meet my condition and keep both of them tracked. Yet I failed to do so. Not to mention my original problem, tracking unknown number of Player objects through while loop. Hope I made my point clear enough now :) – Lam598 Jan 29 '23 at 13:09
  • @knittl hey I am not asking about modifications after compile. Just wondering how to keep track of selected elements in loops. – Lam598 Jan 29 '23 at 13:11

1 Answers1

1

As already answered, variables must be known at compile time. Since you are restricted to use arrays, I'd use second array where you put those players who meet criteria, and give a name using a counter "top1, top2,"etc

// we'll put here those players that meet the criteria. 
// since we do not know how many elements, set size at most same as original array
Player[] bestPlayers = new Player[players.length];

// let's rank the players
int candidate = 0;
for (Player player : players) {
    if (player.getLevel() > 10) {
        // assign a unique name
        player.setName("Top " + candidate);
        bestPlayers[candidate] = player;
        // increase counter of best players
        candidate++;
    }   
}

You can see a working example here: https://www.sololearn.com/compiler-playground/c55gz69GE28O

  • Thanks for your solution! I think I've got the hang of this. One more question if I may? What's the most common way to this problem in industry if I am not restricted to arrays? – Lam598 Jan 31 '23 at 02:47
  • I'm glad I helped you. I'd rather use some implementation of java.utils.Collection, usually an ArrayList or maybe depending on the situation a HashMap – Samuel Pérez Feb 02 '23 at 11:40