项目赢得';使用java.util.Arrays时无法运行--可能会出现什么问题?
当“clearPets”方法中的代码没有被注释掉时,我会遇到一系列错误。只要我删除代码,程序就会以其他方式运行 如何解决这些问题?我最近才了解了如何创建和调用方法,这是我第一次使用项目赢得';使用java.util.Arrays时无法运行--可能会出现什么问题?,java,arrays,boolean,Java,Arrays,Boolean,当“clearPets”方法中的代码没有被注释掉时,我会遇到一系列错误。只要我删除代码,程序就会以其他方式运行 如何解决这些问题?我最近才了解了如何创建和调用方法,这是我第一次使用java.util.array 控制台中的错误有: Exception in thread "main" java.lang.ArrayStoreException: java.lang.Boolean at java.util.Arrays.fill(Unknown Source) at rf.uhh.clearPe
java.util.array
控制台中的错误有:
Exception in thread "main" java.lang.ArrayStoreException: java.lang.Boolean
at java.util.Arrays.fill(Unknown Source)
at rf.uhh.clearPets(uhh.java:34)
at rf.uhh.optionOne(uhh.java:39)
at rf.uhh.main(uhh.java:20)
以下是我的代码:
public class uhh {
public static void main(String[] args){
System.out.println("Select a number");
System.out.println("1");
System.out.println("2");
System.out.print("Choice: ");
Scanner scnr = new Scanner(System.in);
String numberChoice = scnr.nextLine();
if( "1".equals(numberChoice) ) {
System.out.println("You chose 1");
optionOne(new boolean[][] { {false}, {true} });
}
scnr.close();
}
public static boolean[][] adoptPets( int cats, int dogs) {
boolean[][] pets = new boolean[cats][dogs];
return pets ;
}
public static void clearPets( boolean[][]pets) {
Arrays.fill(pets, false);
}
public static void optionOne(boolean[][] center) {
clearPets(center);
boolean[][] dogFaceMan = adoptPets(10, 10);
dogFaceMan[1][1] = true;
}
}
您正在将一个2D数组传递给一个需要1D数组的方法() 试试这个:
public static void clearPets( boolean[][]pets) {
for(int i = 0; i < pets.length; i++) {
Arrays.fill(pets[i], false);
}
}
publicstaticvoidclearpets(boolean[]pets){
对于(int i=0;i
谢谢。这很管用,也很有道理!我会尽快接受这个答案(stackoverflow时间限制)。@Neo没问题,很乐意帮忙。