Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java Language
Expertise: Beginner
Dec 7, 1999

Arrays of Arrays

Question:
Is it possible to have an array of character arrays? What about an array of multidimensional character arrays?

Answer:
Java arrays are first-class objects. They can be referenced by Object reference variables, and unlike C/C++, do not correspond to raw chunks of memory. When you create an array, its contents are not specified unless the array is of a native type. The contents of the array are null object references (there is a short-hand initializer syntax, but that's just a syntactic convenience). You must explicitly initialize the elements of the array.

The types of these elements will depend on the type of the array you declared. If you want to store any arbitrary class instance in the array, including other arrays, you may declare the array to be of type Object[]. But if you want to limit the type of array to store, say, arrays of characters, you would declare it to be of type char[][]. For example:

    char[][] bar;

    bar = new char[50][];

    for(int i=0; i < bar.length; ++i) {
      bar[i] = new char[5];
      for(int j=0; j < bar[i].length; ++j)
        bar[i][j] = 'a';
    }
You can extend this to store multidimensional character arrays in an array.
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date