简体   繁体   English

使用for循环查找数组中的缺失值

[英]Find missing value in array using a for loop

So I am trying to make a for loop that searches through an array and returns how many missing values it has found. 所以我试图做一个for循环,该循环搜索数组并返回找到的缺失值。

Here is my method: 这是我的方法:

    public void questionsMissed() {
            int missedQuestions = 0;
            for (int i = 0; i < 20; i++) {
                if (answers[i] == null){
                    missedQuestions++;
                }
            }
 System.out.println("You have missed " + missedQuestions + " questions.");
        }

The error is where the if statement is. 错误是if语句所在的位置。 I am assuming it is because of the null. 我假设这是因为null。 How can I go around finding missing values? 如何解决缺失的值?

ERROR: incomparable types: int and 错误:无可比拟的类型:int和

You get the error because you are trying to compare a primitive data type, int , to null . 之所以会出现错误,是因为您试图将原始数据类型intnull Only reference types can be compared to null . 只能将引用类型与null进行比较。 Primitive types always have a value - they cannot be null , so the compiler tells you that the comparison does not make sense. 基本类型始终具有一个值-它们不能为null ,因此编译器告诉您比较没有意义。

There are two ways you can go about fixing this: 有两种方法可以解决此问题:

  • Switch the type of answers to Integer - This would let you check its values for null , at the expense of wrapping primitive integers into by-reference wrappers. answers的类型切换为Integer这将使您检查其值是否为null ,但以将原始整数包装到按引用包装器中为代价。
  • You can designate an integer value, say, -1, as "missing" - This would let you keep int as the type of answer[] 's element, but there would be a special number that must be used everywhere in your program. 您可以将一个整数值(例如-1)指定为“ missing”遗漏) -这将使您可以将int作为answer[]元素的类型,但是在程序中的任何地方都必须使用一个特殊的数字。

Since you are using char values for your answers, I would use the second approach, but first I would make answers and correctAnswers arrays of char . 由于您使用的是char值作为答案,因此我将使用第二种方法,但是首先,我将做出answers并设置char correctAnswers数组。 There is a convenient "special value" for the unused value - the null character \\0' : 对于未使用的值,有一个方便的“特殊值”-空字符\\0'

if (answers[i] == '\0') {
    ...
}

Two things :- 两件事情 :-

There is nothing like missing values which you can compare using null. 没有什么像缺失值可以使用null进行比较。 You can compare it to 0 as those defined array members are initialised to 0 (THANKS to dasblinkenlight for correcting me,I was confused with C),but that appears to be a poor decision/choice. 您可以将其与0进行比较,因为那些定义的数组成员被初始化为0(感谢dasblinkenlight来纠正我,我与C混淆了),但这似乎是一个糟糕的决定/选择。

I'd recommend putting -1 for the missing values as is generally done in sentinel condition checking and then compare values like :- 我建议像通常在前哨条件检查中所做的那样,将-1作为缺失值,然后比较类似:-的值。

public void questionsMissed() {
    int missedQuestions = 0;
    for (int i = 0; i < 20; i++) {
        if (answers[i] == -1){
          System.out.println("OOPS,the element here is missing!");
        }
    }
}

You compare a primitive type int to null . 您将原始类型intnull进行比较。 Primitive types are not objects and always have a value. 基本类型不是对象,并且始终具有值。 Thus you can not compare them to null . 因此,您不能将它们与null进行比较。

If you use an Integer[] you can mark missed questions by inserting null or you can compare them with '\\0'. 如果您使用Integer[] ,则可以通过插入null标记未回答的问题,也可以将它们与'\\ 0'进行比较。

But since you already use character literals I would use Character instead. 但是由于您已经使用过字符文字,所以我将使用Character代替。

When you use a primitive wrapper type your methods will also get much easier. 当您使用原始包装类型时,您的方法也将变得更加容易。 Eg 例如

Character[] answers = new Character[20];

....

public void questionsMissed() {
    int missedQuestions = Collections.frequency(Arrays.asList(answers), '\0');
    System.out.println("You have missed " + missedQuestions + " questions.");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM