When declaring an array like this:
int array[][] = {
{1,2,3},
{4,5,6}};
I get an error saying: "Array type has incomplete element type"
What is going on??
Note that as the arrays in Java are dynamically allocated, we do not specify any dimension or size of the array with the declaration. The above declaration tells the compiler that there is an array variable 'myarray' of type int which will be storing the integer type values in it.
One way is to first loop the original input array and set a counter, and then make another loop with that counter length and initialize and fill this array[].
int reqArraySize; printf("Enter the array size: "); scanf("%d", &reqArraySize); After this you may proceed with this interger input array size : for(i=0;i<reqArraySize;i++) scanf("%d",&arr[i]);
Arrays in Java have to have a specific size. If you want a dynamically-sized data structure, use an ArrayList.
With an N-dimensional array (N>0), you need to define the sizes of N-1 dimensions; only one dimension can be left for the compiler to determine, and it must be the first dimension.
You can write:
int d1[] = { ... };
int d2[][2] = { ... };
int d3[][2][3] = { ... };
Etc.
You need to specify all the dimensions except the highest. The reason is that the compiler is going to allocate one big block of memory, as opposed to one array of pointers pointing to their own little arrays. In other words,
int array[][3][4] = ...;
will allocate one contiguous region of memory of size 3*4*(however many 3x4 arrays you declare here). Thus when later on in your code, you write
array[1][2][3] = 69;
in order to find where in memory to write 69, it starts at address (array), then jumps forward 12*sizeof(int) to get to array[1], plus 2*4*sizeof(int) to get to array[1][2], plus 3*sizeof(int) to finally get to the start of array[1][2][3]. Compare this to writing, for example,
int ***array = new int**[n];
for(i=0; i<n; i++)
{
array[i] = new int * [3];
for(j=0; j<4; j++)
array[i][j] = new int[4];
}
(sorry if my syntax isn't exact...been awhile since I've had to code something like this in C). In this example, array points to a block of code n*sizeof(int**) bytes long. Each element of this array points to another array of size 3*sizeof(int*) bytes long. Each element of these arrays points to another array of size 4*sizeof(int) bytes long. In this case, instead of calculating that array[1][2][3] is at address (array + something), it would need to follow a few different pointers in memory before finding where to write 69.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With