如何测试数组以查找其中是否存在某个值?

时间:2017-11-20 06:19:19

标签: java

问题要求我:测试给定的数组,看看其中是否有数字5。如果有5,则返回False。否则,返回true。我得到了它的工作...好吧,我知道它不是一个有效的方式。唯一的问题是,在案例1和案例6中,我得到了Error java.lang.ArrayIndexOutOfBoundsException,我甚至不知道为什么。其余的工作正常。

import java.util.*;

public class testCases
{
    public static void main(String[] args)
    {
        Scanner kb = new Scanner(System.in);

        System.out.print("Which case do you wish to test? ");

        int testCase = kb.nextInt();

        switch(testCase)
        {
            case 1:
                    System.out.println("noFives(1, 2, 3, 4) = " + noFives(1, 2, 3, 4, 6));
                    break;

            case 2:
                    System.out.println("noFives(1, 2, 3, 4, 5) = " + noFives(1, 2, 3, 4, 5));
                    break;

            case 3:
                    System.out.println("noFives(1, 2, 5, 3, 4) = " + noFives(1, 2, 5, 3, 4));
                    break;

            case 4:
                    System.out.println("noFives(5, 1, 2, 3, 4) = " + noFives(5, 1, 2, 3, 4));
                    break;
            case 5:
                    System.out.println("noFives(27, 82, 4, 71, 6, 23, 9, 18) = " + noFives(27, 82, 4, 71, 6, 23, 9, 18));
                    break;

            case 6:
                    System.out.println("noFives(0) = " + noFives(0));
                    break;

            case 7:
                    System.out.println("noFives(5) = " + noFives(5));
                    break;

            default:
                    System.out.println("noFives() = " + noFives());

        }

    }

    public static boolean noFives(int ... n)
    {

        if(n.length == 0)
        return true;

        else if(n[0] == 5 || n[1] == 5 || n[2] == 5 || n[3] == 5 || n[4] == 5 || n[5] == 5 || n[6] == 5 || n[7] == 5)
        return false;

        else
        return true;

    }

}

3 个答案:

答案 0 :(得分:0)

一些因素会影响您实施的不一致行为:

  1. ||运算符短路。即,在noFives()如果n [0] == 5,它不会测试任何其他术语。

  2. 在java中,访问数组末尾之外的元素会导致OutOfBounds异常。

  3. 因此,当||操作链在访问数组外部的索引之前没有短路时,您会收到异常。情况1和6没有五个导致这种短路。

答案 1 :(得分:0)

您应该使用循环来处理n的更改长度。重写代码

import java.util.*;

public class testCases
{
    public static void main(String[] args)
    {
        Scanner kb = new Scanner(System.in);

        System.out.print("Which case do you wish to test? ");

        int testCase = kb.nextInt();

        switch(testCase)
        {
            case 1:
                    System.out.println("noFives(1, 2, 3, 4) = " + noFives(1, 2, 3, 4, 6));
                    break;

            case 2:
                    System.out.println("noFives(1, 2, 3, 4, 5) = " + noFives(1, 2, 3, 4, 5));
                    break;

            case 3:
                    System.out.println("noFives(1, 2, 5, 3, 4) = " + noFives(1, 2, 5, 3, 4));
                    break;

            case 4:
                    System.out.println("noFives(5, 1, 2, 3, 4) = " + noFives(5, 1, 2, 3, 4));
                    break;
            case 5:
                    System.out.println("noFives(27, 82, 4, 71, 6, 23, 9, 18) = " + noFives(27, 82, 4, 71, 6, 23, 9, 18));
                    break;

            case 6:
                    System.out.println("noFives(0) = " + noFives(0));
                    break;

            case 7:
                    System.out.println("noFives(5) = " + noFives(5));
                    break;

            default:
                    System.out.println("noFives() = " + noFives());

        }

    }

    public static boolean noFives(int ... n)
    {

        if(n.length == 0)
        return true;

        else {
            for(int i=0; i < n.length; i++){
                if(n[i] == 5)
                    return false;
            }
        }

        return true;

    }

}

答案 2 :(得分:0)

这是由于参数被发送到方法noFives(int.. n) 对于case语句1,您传递了5个参数

case 1:
                System.out.println("noFives(1, 2, 3, 4) = " + noFives(1, 2, 3, 4, 6));
                break;

而在noFives(int... n)方法中,您检查的索引是否超过5,因此ArrayIndexOutOfBoundsException

 else if (n[0] == 5 || n[1] == 5 || n[2] == 5 || n[3] == 5 || n[4] == 5 || n[5] == 5 || n[6] == 5 || n[7] == 5)

我建议你改变方法noFives(int ..n),如:

public static boolean noFives(int... n) {

        if (n.length == 0)
            return true;

        for (int i = 0; i < n.length ; i++) {
            if (n[i] == 5){
                return false;
            }
        }
        return true;
    }