Forums Register Login

Creating a program that requires user input - should I store in arraylist?

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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[].
+Pie Number of slices to send: Send
Or a list of players (List<Player>).
+Pie Number of slices to send: Send
… and welcome to the Ranch
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:… and welcome to the Ranch



Thank you!

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
And when you are referring to a list that would mean an arraylist, correct?
+Pie Number of slices to send: Send
Well, List in the interface and ArrayList is one implementation. So you would write:

But you could also write:
+Pie Number of slices to send: Send
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.?
+Pie Number of slices to send: Send
Not necessarily. When I referred to a List I meant java.util.List.

This is an interface. It defines some methods each list must have.

It is important to think in interfaces. Don't think ArrayList or LinkedList or MyVeryOwnImplementationOfListInterface.
Just think about a List.

That will prevent you to from bounding your code to a specific implementation.

Of course at some point you'll need to choose an implementation and yes, I would use an ArrayList for this task.

But if I ever wanted to change my mind that would be super easy.

Don't write:
ArrayList<Player> players = new ArrayList<>();
Write:
List<Player> players = new ArrayList<>();

Don't write:
public void printPlayers(ArrayList<Player> players) { }
Write:
public void printPlayers(List<Player> players) { }
+Pie Number of slices to send: Send
Yes, except that you don't give them names. There is no need for player1 player2 and player3 when you can write
playerList.get(0)
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 447 times.
Similar Threads
Help with ArrayList(s) and FileIO
Question on arraylists
Creating a leaderboard.
First Java Game - Help REALLY appreciated
JColor Chooser - JLabels?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:27:41.