Results 1 to 8 of 8
  1. #1
    Splanger is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default Allocate memory for array of Objects.

    Hello,

    I'm confused how to allocate memory for an array, if its members are Objects...

    I tried to create a class CardDeck, which will represent an array of objects of Card class.

    And I get
    Syntax error on token ";", , expected
    error, when I'm trying to allocate memory like that:

    Java Code:
    package app;
    
    class CardDeck {
    	Card[] cards;		
    	static final int deckSize = 36;	
    	cards = new Card[deckSize - 1];
    	
    	/*
    	 * new CardDeck constructor, creates a new CardDeck object
    	 */
    	CardDeck(){
    		int k = 0;		
    		for (int i=0; i<4; i++){
    			for (int j=0; j<8; j++){				
    				cards[k] = new Card(Card.suites[i], Card.rates[j]);
    				k++;
    			}			
    		}
    	}
    	
    	/*
    	 * Outputs all deck cards
    	 */
    	void getDeckDetails(){
    		for(int k = 0; k < (deckSize - 1); k++){
    			this.cards[k].getCardDetails();
    		}
    	}
    I read in the tutorial this lines:
    int[] anArray;
    anArray = new int[10]

    and tried to behave like it was in example.

  2. #2
    billycro is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    well you're doing it right, the one thing i can see that may cause an error is where you create cards, you should make the size deckSize, not deckSize-1, because the size of the array will be the same as the deck, however the maximum index in the array will be deckSize-1

    did that all make sense, im not sure what your question was lol

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default

    What line does the error occur on? Please post the full text of the error message.

    You define an array by using a pair of [] following the type on both sides of the assignment statement:
    AClass[] arrayOfAClass = new AClass[10]; // create an array to hold 10 AClass objects

  4. #4
    Splanger is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    Oh, thanks! It's finally works!

    The error message occur on line with "static final int deckSize = 36;"

    And when I was trying to run program, the full error was:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Syntax error on token ";", , expected

    at app.CardDeck.<init>(CardDeck.java:5)
    at app.First.main(First.java:10)

    By the way,
    I still can't understand, why if I'm declaring array's size for 35 members (4 suites and 9 rates for each suit), that must be equivalent to 36 cards,
    I get a compiler error message:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 35
    at app.CardDeck.<init>(CardDeck.java:14)
    at app.First.main(First.java:10)
    The full code of the CardsDeck class :

    Java Code:
    package app;
    
    class CardDeck {		
    	static final int deckSize = 36;	
    	Card[] cards = new Card[deckSize-1];	
    	
    	/*
    	 * new CardDeck constructor, creates a new CardDeck object
    	 */
    	CardDeck(){
    		int k = 0;		
    		for (int i=0; i<4; i++){
    			for (int j=0; j<=8; j++){				
    				cards[k] = new Card(Card.suites[i], Card.rates[j]);
    				k++;
    			}			
    		}
    	}
    	
    	/*
    	 * Outputs all deck cards
    	 */
    	void getDeckDetails(){
    		for(int k = 0; k < (deckSize-1); k++){
    			this.cards[k].getCardDetails();
    		}
    	}
    	
    }
    and this is the Card class:

    Java Code:
    package app;
    
    class Card {
    	/*
    	 * Initialize Card's class fields
    	 */
    	final String suite;
    	final String rate;
    	static final String[] rates = {"6", "7", "8", "9", "10", "J", "Q", "K", "A"};
    	static final String[] suites = {"Diamonds", "Hearts", "Spades", "Clubs"};
    	
    	Card(String suite, String rate) {
    		this.suite = suite;
    		this.rate = rate;
    	}
    	
    	void getCardDetails(){
    		System.out.println(this.suite + " " + this.rate);
    	}
    }

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default

    ArrayIndexOutOfBoundsException: 35
    Remember arrays are zero based. First index=0, last = arraysize-1
    What is the size of your array?

    BTW The error message is not from the compiler. It comes from execution.

  6. #6
    Splanger is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    Oh, yes... It's my fault, I just missed billycro's answer and after the message was already posted, found his answer))

    My array must contain 36 members, so I suppose, that its size 36.

    Hm... What is the difference between error message from compiler and execution? And how can I distinguish them?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default

    how can I distinguish them?
    Compiler error messages come from the compiler. The javac commmand
    Execution error message come when you execute the program with the java command

    If you are using an IDE that is all hidden from you. I have no idea how your IDE works. Its all magic.

  8. #8
    Splanger is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    I suppose it is good idea for me then, try to program at this stage in the command line)
    Thank you very much!

Similar Threads

  1. How to convert array of Objects into array of Strings
    By elenora in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2011, 03:48 PM
  2. Newbie needs help clearing memory objects
    By mailboxo71 in forum New To Java
    Replies: 10
    Last Post: 01-21-2011, 10:12 AM
  3. Allocate memory
    By thescratchy in forum New To Java
    Replies: 1
    Last Post: 12-17-2010, 10:49 PM
  4. Memory representation of an array?
    By hellolleh in forum Forum Lobby
    Replies: 2
    Last Post: 04-29-2010, 03:05 AM
  5. ow many objects I have open in the memory
    By itaipee in forum Advanced Java
    Replies: 1
    Last Post: 11-23-2009, 09:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •