Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Array of arrays

What are the considerations when using an array of arrays?

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 2

There is an easy-to-use shorthand for initializing array references:

String [][] saa = {
   { { "Hello, World }, { "Guten Tag, Welt"} } };
   // this creates a String[][] object like the one created
   // above, and assigns saa to refer to that object.
   // The whitespace is meant to emphasize that the
   // object created is an array of one String[] which
   // contains two Strings.


Using this shorthand, our example could correctly be written as:

String [][] saa = { { { "Help" } } };


However, this makes saa refer to a one by one string array. Note that the syntax above only works when initializing an array reference (initialization is the special case of assignment at the time of declaration). The more general way to create a new array and assign it to a new or existing array reference looks like this (in the case of an existing reference):

About the author

Random Walk Computing is the largest Java/CORBA consulting boutique in New York, focusing on solutions for the financial enterprise. Known for their leading-edge Java expertise, Random Walk consultants publish and speak about Java in some of the most respected forums in the world.
saa = new String [][] {
// note the empty [][] -- the compiler figures the
// size out (empty [][] is required).
   {  { "Hello" }, { "World" }  }  // this is saa[0]
                                        ,  // note the comma separating
saa[0] and saa[1]
                                        {  { "Guten Tag" },    { "Welt"
}  }  // this is saa[1]
                                    };
    // now saa.length = 2, and saa[0] and saa[1] also each have length 2


  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (2)
Login
Forgot your account info?

use rubyBy Anonymous on July 23, 2009, 11:09 amuse ruby

Reply | Read entire comment

Array + arrayBy Anonymous on July 19, 2009, 6:31 amhow can i do {1,2,3,4} + {5,6} so i get {1,2,3,4,5,6}?

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources