Can someone please explain why I’m getting this error
Type mismatch: cannot convert from String to String[][][]
in this code?
String [][][][] names = {"zach","zach","zach","zach"};
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The variable
is a variable representing a four-dimensional array of strings – that is, an array of arrays of arrays of arrays of strings. The literal
Is a single-dimensional string array with four elements in it. Notice the distinction – an array of four elements is a
String[], not aString[][][][]. A one-dimensional array can have as many elements in it as you’d like. Adding more dimensions to the array is useful if you want to represent something like a 2D or 3D grid, but it’s not the right way to say that the array holds more elements.To fix this, you want to write
This does indeed work correctly.
If you want a 2D array of strings, you could do something like this:
Here, the data is two-dimensional – you select which row you’d like as the first array index, and which column you’d like as the second array index. Notice how the number of array elements in each row and column is independent of the number of dimensions in the array, since these are separate concepts.
Hope this helps!