• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

initializing an array of arrays

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "code block" as shown above is an instance initializer.
 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic