• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Array Sizes

 
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have something like this...
int myInt [][] = new int [3][];
What are the total number of elements in this array. It looks as if its zero. Is it possible to initialize the second array later?
-Dale
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup that's possible. Consider this:
int[][] iArray = new int[2][];
iArray[0] = new int[2];
iArray[1] = new int[10];
This way you can have an array of array of different lengths, which can be useful sometimes..
In this case the total number of elements is 12 (2+10)
[ February 22, 2002: Message edited by: Valentin Crettaz ]
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To answer your first question:
When you execute this statement:
int myInt [][] = new int [3][];
You are creating a two dimensional array with the size of one dimension as 3 that contains null refrences to the other dimension, i.e. even though you are specifying one dimension as 3, this dimension of the array is not initialized with the default primitve values (0 in this case of integer) - it is initialized with null references to the second dimension.
Try out the following code to understand what I am trying to say:

So, I would say there are 3 null references in the array in your example and 5 null references in my example. I do not know if you can classify them as array elements. The array certainly does not have default integer primitive values, i.e. zeroes.
Brian
PS - I just came up with this sentence:
"In a partially constructed primitive multi dimensional array, the dimension that is fully constructed contains null references to the dimension that is not fully constructed. It does not contain default primitive values"
Feel free to correct this statement, keeping in mind my post.
[ February 22, 2002: Message edited by: Brian Lugo ]
[ February 22, 2002: Message edited by: Brian Lugo ]
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is small program I worte to show you what to expect

You may be surprised for the value you get for a.length and ch.length!
output:
a.length = 2
a[0].length = 5
a[1].length = 5
ch.length = 2
ch[0].length = 8
ch[1].length = 12
 
I think I'll just lie down here for a second. And ponder this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic