I'm creating a program that requires a user to input the names, positions and ratings of different players. Would it be best to take in this information as an arraylist, therefore creating a new arraylist for each new player? I would then need to be able to access the information for each individual player.
Knute Snortum wrote:Does a Player has a name, position, and rating? Then maybe it's best to create a Player class and use an array of Players[].
Yes each player will have those values. I had created a player class and passed the three parameters. But with the array or arraylist I'm a little confused. Would I use a muti dimensional array or individual arrays for each player.
You create Player[] (an array of players) or List<Player> (a list of players).
Arrays and lists are similar. The one main difference is arrays have fixed size and lists can grow as you add elements into it (except unmodifiable lists).
You create only one array (or one list) to hold all players. Arrays and lists are containers that store elements (in this case players). You don't create one array per player. That wouldn't make sense. The same is true for multidimensional arrays. Also not applicable here.
Also note that there are no multidimensional arrays in Java. Hovewer you can create an array of arrays.
I would use a list to store players. I find them more convenient to use. Also with arrays you would need to know how many players you will have before creating the array.
I understand how to create an arraylist containing one object in each row, what I don't understand is how to include multiple values. Does that mean I create my player object with a constructor that has the name, position, and rating, and then add each player to the list as player1, player2, payer 3 etc.?