Question about filling arrays and functions inside arrays

Asked

Viewed 84 times

1

insira o código aquiArrayList<Player> players = new ArrayList<Player>(); for(contador=0;contador<x;contador++){ Player p1 = new Player(); p1.setId(contador); p1.setSaldo(valorinteger = Integer.parseInt(parts[2])); players.add(p1); }

1- I need to create X players in this array, and each 1 will have a different balance. how can I do this since it’s not working.

2- I have the array players, and I need to call a function, for example, getsaldo(); from/each of the players in the array. How can I do this? I already appreciate the help, I have a great job to deliver and I know very little java.

  • Correct code. for(counter=0; counter<fixed value;counter++){ Player P1 = new Player(); P1.setId(counter); P1.setSaldo(valorinteger = Integer.parseint(Parts[2]); players.add(P1); }

1 answer

0


Your Player object is a class, you are already setting a set method that assigns the value of the player balance in each instance you are creating in for, to get the value of each player within the players array, just go through the Arraylist with a for and use the getSaldo() of each Player, but for this you will have to create this method within the Player class. Example:

ArrayList<Player> players = new ArrayList<Player>();
    int valorfixo = 5;
    for (int contador = 0; contador < valorfixo; contador++) {
        Player p1 = new Player();
        p1.setId(contador);
        int valorRandomico = (int) (Math.random() * 100);
        p1.setSaldo(valorRandomico);
        players.add(p1);
    }

    for (Player player : players) {
        System.out.println(player.getSaldo());
    }

Player class:

public class Player {
private int id;
private int saldo;
public Player(){

}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public int getSaldo() {
    return saldo;
}
public void setSaldo(int saldo) {
    this.saldo = saldo;
}}
  • Thanks for the help, but I had some questions. What is this (Player : players) { are you doing? Is he running the array player? If it is, can I manually choose a player in the middle of the array to get the balance ex: player[2]. getsaldo? Another question, know how I can host several player objects within the player array?

  • Yes, what is being used is known as for each, it runs through a whole list or array, the player is the object being listed at the time. To catch a specific object in an Arraylist you have to use the players.get(2) method. getSaldo(); For more information on the Arraylist methods see its documentation at the link: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

  • On your last question, you are already placing several instances of the Player object in the players array in the players.add(P1) line. In this case you create an instance of the player: Player P1 = new Player();

  • I understood about for each e get, very clearly its explanation. The last question is about the array’s gone. If it is correct. Because I am creating a player instance that is P1 and I will fill all arraylist positions with it. Once this is done, I can change the values of each P1 within the array with the class functions. Type put the name "A" in P1 that is at position 0, name "b" in P1 that is at position 1, etc. Sorry if I’m not being very clear, I really understand very little of java.

  • Yes, your P1 variable is instantiated with each loop for iteration, so you can get every P1 object you’ve added to your array independent of the other.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.