Multi-dimensional Array Cannot convert int to int[]? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Multi-dimensional Array Cannot convert int to int[]?

As the title says, I keep getting an error saying I cannot convert int to int[]... what does this mean, and what do I have wrong? package practiceClasses; public class SodokuNotGame { public static void main(String[] args) { int[][][] sample = {1,2,3},{4,5,6},{7,8,9}; } }

25th Aug 2021, 6:20 PM
William Davis
William Davis - avatar
9 Answers
0
int [] sample={1,2,3,4,5,6,7,8,9} //You mean like this?
25th Aug 2021, 6:34 PM
Atul [Inactive]
0
@Atul I want it to have an array like this: 1 2 3 4 5 6 7 8 9 So 3x3, 3 rows, and 3 columns
25th Aug 2021, 6:43 PM
William Davis
William Davis - avatar
0
class Program { public static void main(String[] args) { int count=0; int[] arr={1,2,3,4,5,6,7,8,9}; for(int i=0;i<arr.length;i++) { if(count ==2) { count=0; System.out.println (arr[i]); } else { count++; System.out.print (arr[i]+" "); } } } }
25th Aug 2021, 7:07 PM
Atul [Inactive]
0
@Atul So then what is the point of arrays like this: int [][] array = { {1,2,3},{4,5,6} }
25th Aug 2021, 7:23 PM
William Davis
William Davis - avatar
0
int[][][] is 3d array, you need 2d int[][] sample = {{1,2,3},{4,5,6},{7,8,9}};
26th Aug 2021, 1:54 PM
zemiak
0
zemiak Can you explain the difference in an array[] and array[][]
26th Aug 2021, 3:09 PM
William Davis
William Davis - avatar
0
William Davis array[] is a 1d array. Whereas array[][] is a 2d array. And array[][][] is a 3d array
26th Aug 2021, 3:47 PM
Atul [Inactive]
0
//import java.util.Arrays; //here are three simple arrays of numbers int[] arr1 = {1,2,3}; int[] arr2 = {4,5,6}; int[] arr3 = {7,8,9}; //this is array where element is another array int[][] arr2D = {arr1, arr2, arr3}; //similar here int[][] arr2Dx = {{1,2,3}, {4,5,6}, {7,8,9}}; //you can use it for table structure //with row and columns, or for matrix String s1D = Arrays.toString(arr1); String s2D = Arrays.deepToString(arr2D); String s2Dx =Arrays.deepToString(arr2Dx); System.out.println(s1D); System.out.println(s2D); System.out.println(s2Dx);
26th Aug 2021, 3:49 PM
zemiak
0
zemiak Uncomment import java.util
26th Aug 2021, 4:07 PM
Atul [Inactive]