In this Program i am going to display JaggedArray in Java.
Error Message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from int[][] to int[]
at com.lib.JaggedArray.main(JaggedArray.java:9)
Error Description:
In below line i assigned two values for 0th row of an array,so i got an error.
package com.lib;
public class JaggedArray {
public static void main(String[] args)
{
int ari[][]= new int[3][];
ari[0]=new int[8][5];
ari[1]=new int[5];
ari[2]=new int[1];
int count = 0;
for(int b=0; b<ari.length; b++)
{
for(int c=0;c<ari[b].length;c++)
{
ari[b][c]=count++;
}
}
for(int b=0; b<ari.length; b++)
{
for(int c=0; c<ari[b].length; c++)
{
System.out.print(ari[b][c]+" ");
}
System.out.println();
}
}
}
Error Solution:
package com.lib;
public class JaggedArray {
public static void main(String[] args)
{
int ari[][]= new int[3][];
ari[0]=new int[8];
ari[1]=new int[5];
ari[2]=new int[1];
int count = 0;
for(int b=0; b<ari.length; b++)
{
for(int c=0;c<ari[b].length;c++)
{
ari[b][c]=count++;
}
}
for(int b=0; b<ari.length; b++)
{
for(int c=0; c<ari[b].length; c++)
{
System.out.print(ari[b][c]+" ");
}
System.out.println();
}
}
}
Output:
0 1 2 3 4 5 6 7
8 9 10 11 12
13
Post a comment
Please share your valuable feedback and share this article in social media.