Results 1 to 8 of 8
- 07-03-2011, 06:36 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 10
- Rep Power
- 0
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 geterror, when I'm trying to allocate memory like that:Syntax error on token ";", , expected
I read in the tutorial this lines: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(); } }
int[] anArray;
anArray = new int[10]
and tried to behave like it was in example.
- 07-03-2011, 07:00 PM #2
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
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
- 07-03-2011, 07:00 PM #3
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
- 07-03-2011, 08:00 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 10
- Rep Power
- 0
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:
The full code of the CardsDeck class :Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 35
at app.CardDeck.<init>(CardDeck.java:14)
at app.First.main(First.java:10)
and this is the Card 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(); } } }
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); } }
- 07-03-2011, 08:04 PM #5
Remember arrays are zero based. First index=0, last = arraysize-1ArrayIndexOutOfBoundsException: 35
What is the size of your array?
BTW The error message is not from the compiler. It comes from execution.
- 07-03-2011, 08:08 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 10
- Rep Power
- 0
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?
- 07-03-2011, 08:12 PM #7
Compiler error messages come from the compiler. The javac commmandhow can I distinguish them?
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.
- 07-03-2011, 08:15 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
How to convert array of Objects into array of Strings
By elenora in forum Advanced JavaReplies: 1Last Post: 06-10-2011, 03:48 PM -
Newbie needs help clearing memory objects
By mailboxo71 in forum New To JavaReplies: 10Last Post: 01-21-2011, 10:12 AM -
Allocate memory
By thescratchy in forum New To JavaReplies: 1Last Post: 12-17-2010, 10:49 PM -
Memory representation of an array?
By hellolleh in forum Forum LobbyReplies: 2Last Post: 04-29-2010, 03:05 AM -
ow many objects I have open in the memory
By itaipee in forum Advanced JavaReplies: 1Last Post: 11-23-2009, 09:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks