Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes initializing an array of arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "initializing an array of arrays" Watch "initializing an array of arrays" New topic
Author

initializing an array of arrays

Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Ranchers -

I have been reading Just Java 2 (fifth edition, 2002) and found what looked like a beautiful way to initialize arrays (ch. 5, p. 123). The example is:


So I tried it like this:


The result was lots of error messages:
Relatives.java:4: ']' expected
relations[0] = new String[] {"Dad", "Brother", "Sister", "Mom"};
^
Relatives.java:4: <identifier> expected
relations[0] = new String[] {"Dad", "Brother", "Sister", "Mom"};
^
Relatives.java:4: not a statement
relations[0] = new String[] {"Dad", "Brother", "Sister", "Mom"};
^
Relatives.java:4: ';' expected
relations[0] = new String[] {"Dad", "Brother", "Sister", "Mom"};
^
Relatives.java:5: ']' expected
relations[1][0] = "father";
^
Relatives.java:6: ']' expected
relations[1][3] = "mother";
^
6 errors

So I don't understand ... help?

Thanks
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Try this:



Regards,

Pho
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71
Your executable statements are loose in the class body - they should be in an executable block, a constructor, or a method. Only the first line is valid in that context, the declaration of 'relations' as a private member (or property) of the class.
Pho Tek showed the use of a code block that will execute when a new instance of the class is created.


SCJP 1.5
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Pho Tek and Greg -

Thank you very much! Yes, I tried putting the statements in a code block and it worked! I had read that executable statements couldn't be "loose" in a class body, but just hadn't made the association. Now I will probably remember ...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14428
    
  23

The "code block" as shown above is an instance initializer.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Thanks for the reference. Since my whole reason for trying to learn Java was my total incompetence at OOP, this one only took about 15 mins of reading to become a bookmark ...
veda vyas sista
Greenhorn

Joined: Sep 21, 2007
Posts: 10
i have a small doubt you said after the private declaration all the statements should be in braces but why please tell me clearly because it is only a declaration of a string of array but not a method then why to use braces.
veda vyas sista
Greenhorn

Joined: Sep 21, 2007
Posts: 10
one more doubt while printing an array why can't we use or print the elements directly for example :

if i write like this iam getting an error



class Test {
public static void main(String[] argv) {

String a[] = new String[2];
a[0] = "hai";
a[1] = "hello";

System.out.println( a[0], a[1]);

but if i use only one array or i use for loop it is working why ?

iam new to forum if i do any mistakes in posting please forgive me but reply me
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
If I try your program, but instead of this argument list (string, string) I use:
it works.

If you look up the method println in the API, the type of thing printed out is optional, but the number of arguments given is always just one. So if you use a loop of some kind and a series of statements like

it will work.

Your other option is to use the format method of the String class to format your output before you print it.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[veda vyas sista :]why please tell me clearly because it is only a declaration of a string of array but not a method then why to use braces.

Because what the compiler does, after a few prep steps, is the functional equivalent of running some execuatable code.


[ September 23, 2007: Message edited by: Nicholas Jordan ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: initializing an array of arrays