• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help understanding Arrays  RSS feed

 
Jonathan Wallace
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following compiles:

while the following doesn't:

What I don't understand is:

1) Why does the first example compile at all? Given that square is a 2D array, it seems it shouldn't be possible to assign a value to the array without explicitly including both dimensions (e.g. int[2][1]).

2) How come the second example doesn't compile, when the assignment is done the exact same way?
 
Michael Gomez
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your array declaration says 'make an array containing other int arrays'.

So assigning a new array of integers makes sense: 'new int[]'.
But in your second example you try to assign an integer ('2') to an array that can only hold integer arrays.
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given that square is a 2D array, it seems it shouldn't be possible to assign a value to the array without explicitly including both dimensions (e.g. int[2][1]).

That's because square is not a 2D array. It's an array of arrays. Java does not have higher dimensional arrays.
 
Jonathan Wallace
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you guys sure?

Michael Romero wrote:Your array declaration says 'make an array containing other int arrays'.

So assigning a new array of integers makes sense: 'new int[]'.
But in your second example you try to assign an integer ('2') to an array that can only hold integer arrays.


The declaration part is only "int[][] square" which would seem to create a 2D array. For instance, the following compiles:

----
Stephan van Hulst wrote:Given that square is a 2D array, it seems it shouldn't be possible to assign a value to the array without explicitly including both dimensions (e.g. int[2][1]).
That's because square is not a 2D array. It's an array of arrays. Java does not have higher dimensional arrays.


What would you consider to be a 2D array? The example above (square[0][0]) seems very much 2D to me. Then 3D arrayName[][][] and so on.
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My definition of a two dimensional array is a structure which contains one data type and requires two coordinates to retrieve a sample of the data. Java does allow you to retrieve elements from arrays using two coordinates, e.g. you can not do something like int i = square[x,y]; instead, you first retrieve an array from the array of arrays, and then you retrieve the data from that array:
Shorthand, this makes int i = square[x][y];
Test it, you can even do int i = (square[x])[y];
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this: In C, when you want to get a value from an MxN array, C just performs x*N+y and then it returns the value at that address. It's one solid block of memory.

In Java, an array is always linear, and deeper arrays can be in completely different parts of the memory.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In this case of array of array's you first defined the size of an array and then length of an each array.

;
But in your second example you defined the size of array not defined the length an array and tried to assign value that's why it is giving an error.

Or in simple language, array is always represented by an object. When you defined the size of an array then it creates that many object and when you give length of an array then it defined that much length into that object. Then we can assign any value to it but if we tried to access that memory location without defining it that then it will give error.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonathan Wallace wrote:
Stephan van Hulst wrote:Given that square is a 2D array, it seems it shouldn't be possible to assign a value to the array without explicitly including both dimensions (e.g. int[2][1]).
That's because square is not a 2D array. It's an array of arrays. Java does not have higher dimensional arrays.

What would you consider to be a 2D array? The example above (square[0][0]) seems very much 2D to me. Then 3D arrayName[][][] and so on.


IMO, a 2D array (meaning a real one), should be as supported by other languages such as C; they allocates one contiguous memory space for the array; the width and length of the array is fixed; etc.

With Java, it is an array of array, because it is not one array. The first array is an array of int array references. And each reference points to an array of ints. Also, when you code something like new int[2][1], it is actually syntactic sugar. Java will generate code that instantiate the array of int arrays, and code that loop through and instantiate all the int arrays.

Henry


[EDIT: Just noticed that Stephan said basically the same thing. Sorry for the repeat.]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!