• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing a multidimensional array from another class  RSS feed

 
wayne morton
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a follow on from a previous question which i resolved here
http://www.coderanch.com/t/589484/java/java/Accessing-array-seperate-class

As i have requested it to be marked as resolved i am not sure if people would bother looking at it anymore so started a new thread. Not sure of the etiquette in this situation but onto my question...

So i want to achieve exactly the same principle as the previous question just with a multidimensional array and have just expanded my previously working code to a multidimensional array as below
Referencing class


Array class

I get an error on lines 08 and 09 saying:
cannot find symbol
symbol: class choices
location: class MyArrays

']' expected

cannot find symbol
symbol: class one
location: class MyArrays

<identifier> expected

So my questions in order of precedence are:-
How do i get it to work with a multidimensional array?

Why does it work with a one dimensional array and not a multidimensional array?

Where does it expect ']' to be?


Thanks for the help.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wayne morton wrote:So my questions in order of precedence are:-
How do i get it to work with a multidimensional array?

Actually, your question should be: why doesn't it work?

And the simple reason is that those statements aren't declaratiive.
You can't just put execution code anywhere you like; it has to be inside a method, or inside a block. Yours is neither.

Winston
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wayne morton wrote:
Array class

I get an error on lines 08 and 09 saying:
cannot find symbol
symbol: class choices
location: class MyArrays

']' expected

cannot find symbol
symbol: class one
location: class MyArrays

<identifier> expected



You can't just have code anywhere. They must be in an initializer, constructor, or method. Additionally, you are also allowed to do an init assignment during declaration of fields.

At line 8 and 9, you have some code that don't follow that rule. And the compiler don't know what to do with it.

[EDIT: beaten to the answer again.... ]

Henry
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error has nothing to do with arrays. You can't have free floating statements like that outside of a code block. That is, lines 8 and 9 need to be inside a method or an initialization block. If you put braces around lines 8 and 9, your code should compile.

You don't need the MyArrays class. A Java array is already a very rudimentary kind of class in that you can call getClass() on an array reference and access the 'length' property of an array. You don't need to wrap an array within another class called MyArrays unless you want to add more behavior around it. You're just making things hard on yourself. What you've written is like Russian Dolls, only with an array.

Edit: moderator pile on!
 
wayne morton
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Actually, your question should be: why doesn't it work?

And the simple reason is that those statements aren't declaratiive.
You can't just put execution code anywhere you like; it has to be inside a method, or inside a block. Yours is neither.

Winston


Why doesn't it work is encompassed in why does it work with a one dimensional array and not a multidimensional array?
You haven't answered that?

Why in a very basic example does this work

but this gives an error

?
Why is one array declarative and the other not?
Why is one array considered execution code and the other not?


I already tried putting my multidimensional array inside a method.e.g.


Then my array seems ok but i can't then transfer it into the Test class as getArray() in the MyArrays class can't find choices and the test class can't find choices.

Care to give me an example of how it should look and then maybe i will understand the differences a bit better?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wayne morton wrote:Why doesn't it work is encompassed in why does it work with a one dimensional array and not a multidimensional array?
You haven't answered that?

Actually, I have. Unless you can show me an example where it works for a one-dimensional array (and believe me, you won't be able to), then it doesn't work for any type of array; indeed for any type of field.

Why in a very basic example does this workbut this gives an error?

Simple: because one is a valid declaration, and the other isn't.

Why is one array declarative and the other not?

I think you probably need to re-read the tutorials, but here's my question to you: What do you think
String [3] choices;
declares?

I already tried putting my multidimensional array inside a method.e.g.Then my array seems ok but i can't then transfer it into the Test class as getArray() in the MyArrays class can't find choices and the test class can't find choices.

Care to give me an example of how it should look and then maybe i will understand the differences a bit better?

Sure:
Does that make it any clearer?

Winston
 
wayne morton
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your replies and taking your time to deal with someone who is new and learning. I think i understand a lot better now.

From what you have shown me, it's about where you declare the array?
i.e.Because the array is declared outside of the method the Test class and getArray() can now reference it while you can populate the array inside the method?


In reference to the one dimensional array, the reason my previous example worked is because it is constructed differently.i.e. it is populated at the declaration phase instead of being declared and then populated?


Junilu Lacar wrote:There error has nothing to do with arrays. You can't have free floating statements like that outside of a code block. That is, lines 8 and 9 need to be inside a method or an initialization block. If you put braces around lines 8 and 9, your code should compile.

You don't need the MyArrays class. A Java array is already a very rudimentary kind of class in that you can call getClass() on an array reference and access the 'length' property of an array. You don't need to wrap an array within another class called MyArrays unless you want to add more behavior around it. You're just making things hard on yourself. What you've written is like Russian Dolls, only with an array.

Edit: moderator pile on!


I didn't really realise that they were separate statements in their own right instead of being part of the array so to speak. that is a good lesson to learn and keep in mind for the future.i.e. a statement is a statement no matter what it is tagged along with.

So it is best to keep relevant arrays inside the class that is using them?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wayne morton wrote:
From what you have shown me, it's about where you declare the array?
...
So it is best to keep relevant arrays inside the class that is using them?

Yes, the idea is to encapsulate the data with the logic that uses it. This helps hide implementation details from any other code that uses the class. Other code should only be concerned with external behavior, not the internal implementation. This contributes to robustness in your code because you can change the internal implementation without fear of affecting too many other things that are dependent on the behavior of the code.

In this example, you don't care if there's an array of strings or whatever in GoodClass. You're just interested in getting a string that is basically a random value. A good class like GoodClass makes it irrelevant to the client code whether or not the value comes from an array. My colleague likes to say "It could have been delivered by a fuzzy bunny riding a unicorn over a rainbow for all we know."
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!