• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on arraylists  RSS feed

 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am trying to store a number of different value cards for a number of players. E.g there can be 2 , 4 or more players and each player can have any number of cards.

I have decided to use an arraylist for cards and tried using an array for players. But after coding and reading some information online, i realised that it is not possible to have an array of arraylist. could anyone guide me on this approach?
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who told you that it is impossible to have an array of Arraylist? That's not true. You can have array of Arraylist.
This compiles with no problems
But for your needs I would suggest a Map that maps from whatever identifies your player (player number, some string, Player object?) to your ArrayList of cards.

Just replace Card and Player with classes you are using.
 
Bob Ivanovich
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create a normal ArrayList and store player objects within it. Each player object would have multiple properties that you can access such as number of cards, position etc...

This might not be exactly what you were looking for but I think it can work.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Ivanovich wrote:You could create a normal ArrayList and store player objects within it. Each player object would have multiple properties that you can access such as number of cards, position etc...

+1 to that. A Player object should hold list of cards. It's better than what I wrote
 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am using a class Card which stores the value of the card.
public Class{
int cardValue;

public Card(int myValue){
cardValue=myValue;
}
}

Arraylist<Card> list=new Arraylist<Card>();

When you mean a player object of arraylist which stores a number of different value cards do you mean the below?
Arraylist<Object> player=new Arraylist<Object>();
??->player.list.add(new Card (sc.nextInt()));
i am not sure how to link them together
 
Bob Ivanovich
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do it that way I am not sure how you would be able to access a certain player. If you don't care about the certain player and how much cards he has you can do what you just said however if you need to access a specific player & how many cards he has you would need to do something like this:



And the player class's constructor would take all the info such as amount of cards. It would also have a set method and a get method for the amount of cards incase you would need to modify that information.
 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Ivanovich wrote:If you do it that way I am not sure how you would be able to access a certain player. If you don't care about the certain player and how much cards he has you can do what you just said however if you need to access a specific player & how many cards he has you would need to do something like this:

x.add(new player(noOfCards));

And the player class's constructor would take all the info such as amount of cards. It would also have a set method and a get method for the amount of cards incase you would need to modify that information.


how do i store for example 100 cards for player 1, 200 cards for player 2, 300 cards for player 3 and so on. Each of the cards could be of any value.
because from what i understand, the above code you typed tells me only many players in an arraylist and the amount of cards each player has. how do i implement another arraylist to store the different values for each card of the player?
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Who told you that it is impossible to have an array of Arraylist? That's not true. You can have array of Arraylist.
This compiles with no problems

Well, surely OP meant, he can't have an array of parameterized ArrayList, which is true. The way you have created an array is using raw type ArrayList, which although compiles, gives an Unchecked Warning. Basically, one should try to avoid mixing arrays and collections for the way they enforce type check on the elements they store.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
benjamin parker wrote:how do i store for example 100 cards for player 1, 200 cards for player 2, 300 cards for player 3 and so on.

You'll need 2 classes here - Card and Player. Now, each player would have a list of card. That you do by having a List<Card> reference in Player class. And then, for each card that comes, you create a Card object, and add it to the List<Card> for a particular Player.
 
Bob Ivanovich
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a fast example I wrote (Note: the constructor for the Game class is very ugly and you shouldn't do all this just in the constructor. I only did this to give you an idea)

Game class: (takes player num and amount of cards & sends them to arrayList "x" as a player object)



Player class: (includes all the methods I previously told you about)

 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Ivanovich wrote:Here is a fast example I wrote (Note: the constructor for the Game class is very ugly and you shouldn't do all this just in the constructor. I only did this to give you an idea)

Game class: (takes player num and amount of cards & sends them to arrayList "x" as a player object)



Player class: (includes all the methods I previously told you about)



sorry this wasn't whati was asking
 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:
benjamin parker wrote:how do i store for example 100 cards for player 1, 200 cards for player 2, 300 cards for player 3 and so on.

You'll need 2 classes here - Card and Player. Now, each player would have a list of card. That you do by having a List<Card> reference in Player class. And then, for each card that comes, you create a Card object, and add it to the List<Card> for a particular Player.

i have typed out the code below. i am having difficulty understand what you mean by having a list<card> reference in player class, do i call it in the constructor? so far for programs i have done with arraylist are all single class so it's a little tricky when i try to call them from another to combine . Also i am only allowed to use arraylist for this program



[Edit: removed overly long lines after the end of the code that added no value, but were causing the wrapping problems]
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Ivanovich wrote:Here is a fast example I wrote (Note: the constructor for the Game class is very ugly and you shouldn't do all this just in the constructor. I only did this to give you an idea)

Although it would have been better if you yourself didn't created such constructor. In how much hurry you may be, that is no excuse for such code

Bob Ivanovich wrote:Game class: (takes player num and amount of cards & sends them to arrayList "x" as a player object)

I guess you're missing an important requirement from OP. you should not just store the number of cards as int. Each Card have a separate value, while you're considering each of them having a value of 1. So, that wouldn't work.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
benjamin parker wrote:
i have typed out the code below. i am having difficulty understand what you mean by having a list<card> reference in player class as so far for programs. I have done with arraylist are all single class so it's a little tricky when i try to call them from another to combine .
Also i am only allowed to use arraylist for this program



Why are you having the cardNo attribute here? You already are storing the value of card in the cardValue attribute of Card. What I meant by List<Card> is this:


benjamin parker wrote:

Whenever you read an input from user, you should ensure first that there is actually some input to read, else you might get a NoSuchElementException at runtime. Look into the Scanner#hasNextInt() method for that.

benjamin parker wrote:

Why having an array and an ArrayList both? Just an ArrayList<Player> should be fine.

benjamin parker wrote:

You don't need it here now. As you would storing the List<Card> in the Player class.
 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry as mentioned i should only solve this using arraylist but not list as i wasn't taught on that yet as i have just started on java not long ago. i understand that that i should check for input, for now we are assuming that user has entered valid inputs.

i have also removed the array as earlier i was trying to see if array of arraylists works.

How do i link both of them up if the solution(using list) you proposed is not used?
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
benjamin parker wrote:sorry as mentioned i should only solve this using arraylist but not list as i wasn't taught on that

Actually, that is using an ArrayList only. List is just an interface which ArrayList implements. If you haven't been taught about interfaces, then you can change List<Card> to ArrayList<Card>. That would work too.
 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Do you mean something like this?

[Edit: removed overly long lines after the end of the code that added no value, but were causing the wrapping problems]
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
benjamin parker wrote:
Do you mean something like this?

No. Give a second look onto my reply. Identify where am I putting the ArrayList<Card>? You're putting it completely outside. You're still passing some cardValue to the Player constructor, which I don't think you want. You should first set the cardValue to a Card object, and then add that Card object to the ArrayList<Card> for a Player

Here's a sample code, which will give you an idea of how you do that:


This code can be further improvised. Which we can look at once you get it working.
 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:
benjamin parker wrote:
Do you mean something like this?

No. Give a second look onto my reply. Identify where am I putting the ArrayList<Card>? You're putting it completely outside. You're still passing some cardValue to the Player constructor, which I don't think you want. You should first set the cardValue to a Card object, and then add that Card object to the ArrayList<Card> for a Player

Here's a sample code, which will give you an idea of how you do that:


This code can be further improvised. Which we can look at once you get it working.


okay it appears what i thought in mind wasn't the right approach. after seeing your code, i understood what you meant.

for the accessor in Player class, is this the right method?
public ArrayList getCards(){
return cards;
}
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Just make the return type parameterized - ArrayList<Card>.
 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:Yes. Just make the return type parameterized - ArrayList<Card>.


What does this error mean? It's coming from the getCards() method

:12: error: cannot find symbol
return cards;
^
symbol: variable cards
location: class Player

ok think i fixed it as i was missing the attributes
 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this the right way to print out the players and value of cards? Do i need to add a override toString method in the Player class?
Printed out format example
Player 1
620
200
Player 2
220


 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally you need two loops, one for the list of players and one for the list of cards. However, if you override toString in the Card class you can print out the card values just by printing out the arraylist holding them (no iteration required).
If you override toString() in the Player class you can also print out that player's name or number without much trouble. Then you can do something like this:

for (Player player : playerList) {
System.out.println(player + player.getCards());
}
 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:Generally you need two loops, one for the list of players and one for the list of cards. However, if you override toString in the Card class you can print out the card values just by printing out the arraylist holding them (no iteration required).
If you override toString() in the Player class you can also print out that player's name or number without much trouble. Then you can do something like this:

for (Player player : playerList) {
System.out.println(player + player.getCards());
}


i'm in the learning process so i'll try out both method. I would like to clarify something, for the first method is something like this correct also do i have to add an accessor for the player as well since the getCards only returns the cards



Also i believe i should create a arraylist of player but R.jain's recent code just made an object of player. Should it be changed to something below?
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For each player in your player list you need to get that player. Then for each card that player has in his list of cards you need to get that card. Then you can print out the result. Your code is incorrect because you're trying to get cards from the list of players as a whole instead of a player in that list that you've picked out.
 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:For each player in your player list you need to get that player. Then for each card that player has in his list of cards you need to get that card. Then you can print out the result. Your code is incorrect because you're trying to get cards from the list of players as a whole instead of a player in that list that you've picked out.

can i know which part is incorrect?
 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm stuck at here now and i am not sure if this is correct. I'm can't seem to get the cards to be stored when i tried to get it's size.
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
benjamin parker wrote:I'm stuck at here now and i am not sure if this is correct. I'm can't seem to get the cards to be stored when i tried to get it's size.


You're creating two new Player objects (one in line 44, one in line 51). Which one has the cards,and which one doesn't? And which one are you adding to the list?
Hint: Passing in a Player to a constructor doesn't copy the object. Your constructor just throws it away, as it isn't used.
 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:
benjamin parker wrote:I'm stuck at here now and i am not sure if this is correct. I'm can't seem to get the cards to be stored when i tried to get it's size.


You're creating two new Player objects (one in line 44, one in line 51). Which one has the cards,and which one doesn't? And which one are you adding to the list?
Hint: Passing in a Player to a constructor doesn't copy the object. Your constructor just throws it away, as it isn't used.


player class that can hold the arraylist of cards (line44)
line51 is the arraylist of player objects.
That was what i am trying to do. So it's wrong? If so how do i go about correcting?
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
benjamin parker wrote:

player class that can hold the arraylist of cards (line44)
line51 is the arraylist of player objects.
That was what i am trying to do. So it's wrong? If so how do i go about correcting?


Try adding testPlayer to the list, not a new player object
 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:
benjamin parker wrote:

player class that can hold the arraylist of cards (line44)
line51 is the arraylist of player objects.
That was what i am trying to do. So it's wrong? If so how do i go about correcting?


Try adding testPlayer to the list, not a new player object

Ok i did as you said. This is my current output. Any idea why it is printed as Card@7dabdb8a rather than 44 instead?

2
1
66
3
55
44
88
Player1
Card@9ffdf86
Player2
Card@7dabdb8a
Card@7dabdb8a
Card@7dabdb8a
2
1
3
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. This is the output of the standard implementation of the toString() method. But you can just override it for the Card class, like this:

 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote:Yup. This is the output of the standard implementation of the toString() method. But you can just override it for the Card class, like this:


Do i still need line 53 to 59 in the main class if i were to use a overside tostring method as i was trying the method 1 from the website below.

http://voices.yahoo.com/java-tips-three-ways-iterate-through-arraylist-12321909.html?cat=15
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you do, as you are iterating over two collections. Method 1 is just another way of using the for loop, this is called the enhanced for loop (or for each). It is described here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

This means you could do your for loops differently, but you'd still need both of them.
Well, the second one is more or less optional, as Tyson remarked, but it would change your output. You could just use System.out.println(playerList.get(i).getCards()).

EDIT: Whoops, I confused methods 1 and 3. Well, method 1 is what I described in my last sentence
 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks managed to get it working, time to add more methods to solve this question
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
benjamin parker wrote:ok thanks managed to get it working, time to add more methods to solve this question

Great. May be you can add your current working code here, for further reference.
 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nullnullnullnull
 
benjamin parker
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
benjamin parker wrote:
R. Jain wrote:
benjamin parker wrote:ok thanks managed to get it working, time to add more methods to solve this question

Great. May be you can add your current working code here, for further reference.

sure will post once i finish the entire code as this is kinda the skeleton only. the important stuff are right behind.

Right now i have a question. For this program, let's say i have 10 players who are playing this game. They will all put down the first card from their hand on the table. The player with the biggest card takes all the cards on the table and continues playing with his current card while the confiscated cards are not allowed to be used again and are rearranged in descending order and placed at the back of his hand.

i am thinking of coverting all 10 players hand to an array and using a method to compare the value of current card for each player. After determining the winner, the arraylist will add a null value at the end of the winner's arraylist before adding the confiscated cards. Is my idea feasible and is it efficient to have 10 arrays for 10 players despite having the arraylist or there is another way to tackle this?
At the start
player1 = 515 335
player2 = 147 815 389
player3 = 11 347 15000
round 1
player1 = 515 335 147* 11*
player2 = 815 389
player3 = 347 15000
round 2
player1 = 335 147* 11*
player2 = 815 389 515* 347*
player3 = 15000
round 3 - Game ends as since player1 finshed his all original cards in his hand
player1 = 147* 11*
player2 = 389 515* 347*
player3 = 15000 335* 815*
* refers to cards that have been played

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!