playerList and it's Player

Hi, my question is how can i use the players added to the arraylist? Because i didn’t created the players one by one and added them also one by one to the arraylist i think i can only access them by their position in the arraylist isn’t it so? if so, how to it? and how to use the Player instances methods?

    playerList.addPlayer(new Player("Pedro"));
    playerList.addPlayer(new Player("Miguel"));
    playerList.addPlayer(new Player("Afonso"));
    playerList.addPlayer(new Player("Paulo"));

Sry, but i’m still at the very beginning of programming and getting very confused lol. Thanks in advance.

Hello…

Well, you have two ways of retrieving data from an ArrayList:

1st: Using its index:

Player p1 = playerList.get(0); Player p2 = playerList.get(1); // and so on...

2nd: Using a for loop:

for(Player p : playerList) { System.out.println(p); // do something util here }

Hope I’ve helped… :thumbup: