What is the difference between a multidimensional array in java and C ++?

char a[][4] = {{'a','a','a'},
               {'a','a','a'},
               {'a','a','a'},
               {'a','a','a'}};

In C ++, you must initialize the last index of the array.

In java, if you do this, it will result in an error.

Can someone explain to me why?

Also, when I check the length of an array, like this in java.

System.out.println(a[0].length);

the result is the length of the column in the array

lets say that at the end of the array there is "\ 0", and when I check the length of the array in C ++, like this.

cout << strlen(a[0]) 

I get the length of the whole array.

Can I just get the length of a row or column in an array in C ++, like in java?

Is this becasase array in java an object?

+3
source share
2 answers

Java , ( , ). ++ , , . ++ , , , , .

, Java, ++, , ( Java ++). , , Java .

+9

, , :

template <typename T, size_t N>
size_t array_size( T (&)[N] ) {
   return N;
}

:

char a[][4] = {{'a','a','a'},
               {'a','a','a'},
               {'a','a','a'},
               {'a','a','a'}};
std::cout << array_size(a)    << std::endl;    // 4
std::cout << array_size(a[0]) << std::endl;    // 4

, , , . 4 ( , ). 4 3 , : char a[][3].

+2

All Articles