This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Mike O'Leary
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[].
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or a list of players (List<Player>).
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… and welcome to the Ranch
 
Mike O'Leary
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike O'Leary
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And when you are referring to a list that would mean an arraylist, correct?
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, List in the interface and ArrayList is one implementation. So you would write:

But you could also write:
 
Mike O'Leary
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.?
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) { }
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!