5 Replies - 282 Views - Last Post: 25 January 2013 - 11:30 PM Rate Topic: ***-- 2 Votes

#1 Helppppp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-January 13

ArrayList, java

Posted 25 January 2013 - 09:38 PM

I have problem to change the array to ArrayList.
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
		} 
	} 


Is This A Good Question/Topic? 0
  • +

Replies To: ArrayList, java

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,614
  • Joined: 27-December 08

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.
Was This Post Helpful? 0
  • +
  • -

#3 Helppppp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-January 13

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){
		
	}

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,614
  • Joined: 27-December 08

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.
Was This Post Helpful? 0
  • +
  • -

#5 Helppppp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-January 13

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
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,614
  • Joined: 27-December 08

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1