ArrayList<Card> deck = new ArrayList<Card>();
public void createDeck(){
for (int i=0; i<families.length; i++){
for (int k=0; k<members.length; k++){
deck[i * 4 + k] = new Card(families[i], members[k],occupations[i]);
} // the "deck[i*4+k]" has to change to ArrayList type
}
}
ArrayList, java
Page 1 of 15 Replies - 122 Views - Last Post: 25 January 2013 - 11:30 PM
#1
ArrayList, java
Posted 25 January 2013 - 09:38 PM
I have problem to change the array to ArrayList.
Replies To: ArrayList, java
#2
Re: ArrayList, java
Posted 25 January 2013 - 09:42 PM
See the ArrayList add() method. You really don't need to specify the indices to add the elements though, but you can.
ArrayList add(int index, E element)
ArrayList add(E element)
The documentation has more information.
ArrayList add(int index, E element)
ArrayList add(E element)
The documentation has more information.
#3
Re: ArrayList, java
Posted 25 January 2013 - 11:19 PM
The code for the deck by using ArrayList:
import java.util.ArrayList;
public class Deck {
ArrayList<Card> deck = new ArrayList<Card>();
public static final int TOTALFAMILIES = 12;
public static final int TOTALCARDS = TOTALFAMILIES * 4;
private int cardsDispensed = 0;
String [] members={"Father", "Mother", "Son", "Daughter"};
String [] families={"Back","Carr","Duck", "Mike",
"Pug", "Putt", "Salt", "Snap",
"Strong", "Swift", "Weed", "Wing"};
String [] occupations={"Footballer", "Motorist", "Cricketer",
"Announcer", "Boxer", "Golfer", "Boatman",
"Photographer", "Weight Lifter", "Runner",
"Gardener", "Airman"};
//How can i change the code from an array to arraylist?
public void createDeck(){
for (int i=0; i<families.length; i++){
for (int k=0; k<members.length; k++){
deck[i * 4 + k] = new Card(families[i], members[k],occupations[i]);
}
}
}
public void shuffleDeck(){
for (int i=0; i<1000; i++){
int pos = new java.util.Random().nextInt(TOTALCARDS);
Card temp = deck[pos];
deck[pos] = deck[0];
deck[0] = temp;
}
//Anyone knows how does this method mean and how can I do it in Happy Family Game which I have already got a createDeck method??
public Card createCard(String, String){
}
#4
Re: ArrayList, java
Posted 25 January 2013 - 11:22 PM
Have you actually bothered to do any research on how to use an ArrayList? The documentation shows you how to use the ArrayList class.
Also, this public Card createCard(String, String){ is not a valid method header. The Strings need valid variable names, not just type.
Also, this public Card createCard(String, String){ is not a valid method header. The Strings need valid variable names, not just type.
#5
Re: ArrayList, java
Posted 25 January 2013 - 11:25 PM
As I do not know what does it actually means that's why I did not put any variables in it
#6
Re: ArrayList, java
Posted 25 January 2013 - 11:30 PM
Quote
As I do not know what does it actually means that's why I did not put any variables in it
Your code doesn't compile. Run your code and you will see what I mean. Method parameters must have names.
This is illegal syntax:
public void foo(int){}
While this is legal syntax:
public void foo(int x){}
Reading material:
ArrayList vs. Static Arrays
Defining Methods
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|