在java中使用Arrays.fill方法分配默认值时发生ArrayStoreException
尝试使用Arrays.fill方法分配默认值,但我得到了ArrayStoreException。下面给出了代码“用于在2d数组中查找从源到目标的所有路径,只需移动k次” 我浏览了几个links(),但找不到问题所在在java中使用Arrays.fill方法分配默认值时发生ArrayStoreException,java,Java,尝试使用Arrays.fill方法分配默认值,但我得到了ArrayStoreException。下面给出了代码“用于在2d数组中查找从源到目标的所有路径,只需移动k次” 我浏览了几个links(),但找不到问题所在 public class FindPathWithConstaint { int[][][][] dp = new int[100][100][100][2]; public static void main(String arg[]) { int m
public class FindPathWithConstaint {
int[][][][] dp = new int[100][100][100][2];
public static void main(String arg[]) {
int m = 3, n = 3, k =2;
FindPathWithConstaint obj = new FindPathWithConstaint();
System.out.println(obj.findAllPath(m,n,k));
}
public int findAllPath(int m, int n, int k) {
if(m==0 && n==0)
return 1;
Arrays.fill(dp,-1);
return countAllPaths(m-1,n,k,1) + countAllPaths(m,n-1,k,0);
}
public int countAllPaths(int i, int j, int k, int d) {
if(i<0 || j<0)
return 0;
if(i==0 && j==0)
return 1;
if(k==0) {
if(d==0 && i==0)
return 1;
if(d==1 && j==0)
return 1;
}
if(dp[i][j][k][d]!=-1)
return dp[i][j][k][d];
if(d==0)
return countAllPaths(i,j-1,k,0) + countAllPaths(i-1,j,k-1,1);
else
return countAllPaths(i-1,j,k,1) + countAllPaths(i,j-1,k-1,0);
}
}
公共类使用Constaint查找路径{
int[]dp=新int[100][100][100][2];
公共静态void main(字符串arg[]){
int m=3,n=3,k=2;
FindPathWithConstaint obj=新的FindPathWithConstaint();
System.out.println(对象findAllPath(m,n,k));
}
公共int findAllPath(int m,int n,int k){
如果(m==0&&n==0)
返回1;
数组填充(dp,-1);
返回countallpath(m-1,n,k,1)+countallpath(m,n-1,k,0);
}
公共int countallpath(int i,int j,int k,int d){
如果(i由于在这一行中将dp声明为多维数组,所以出现此错误
int[][][][] dp = new int[100][100][100][2];
这意味着您将拥有一个数组数组,然后您尝试使用arrays.fill()
将值分配给该数组,这会在此行中引发错误
Arrays.fill(dp,-1);
它引发异常,因为fill
方法试图对dp数组的每个元素影响一个整数“-1”,尽管dp是数组数组数组而不是整数数组,这正是异常的原因,您之所以出现此错误,是因为您在这一行中将dp声明为多维数组
int[][][][] dp = new int[100][100][100][2];
这意味着您将拥有一个数组数组,然后您尝试使用arrays.fill()
将值分配给该数组,这会在此行中引发错误
Arrays.fill(dp,-1);
它引发异常,因为fill
方法试图对dp数组的每个元素影响一个整数“-1”,尽管dp是数组数组数组而不是整数数组,这正是异常的原因,感谢注意原因,问题解决感谢注意原因,问题解决