File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initializing an array of arrays

 
Jinny Morris
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 782
Chrome Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

 
greg buela
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jinny Morris
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "code block" as shown above is an instance initializer.
 
Jinny Morris
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1282
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic