Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java for循环是导致此代码中出现错误的原因吗?_Java_Arrays_Debugging_For Loop - Fatal编程技术网

Java for循环是导致此代码中出现错误的原因吗?

Java for循环是导致此代码中出现错误的原因吗?,java,arrays,debugging,for-loop,Java,Arrays,Debugging,For Loop,所以对于我代码的这一部分,我想知道为什么我的测试类不能工作。我严格按照说明操作,但我不知道为什么我会出错。 基本上这两个部分,, 如果数组参数为null,则它必须抛出带有消息“array is null”的BadArrayException。 如果数组参数的长度为0,则它必须返回-1。 它不能更改数组参数内容,也不能将整个数组参数内容复制到另一个数组。 要查找第一个匹配项,它必须搜索数组参数一次。 它不读取或打印任何内容 那么我应该首先修正intvalue=0的方式吗?还是我的for循环有问题?

所以对于我代码的这一部分,我想知道为什么我的测试类不能工作。我严格按照说明操作,但我不知道为什么我会出错。 基本上这两个部分,, 如果数组参数为null,则它必须抛出带有消息“array is null”的BadArrayException。 如果数组参数的长度为0,则它必须返回-1。 它不能更改数组参数内容,也不能将整个数组参数内容复制到另一个数组。 要查找第一个匹配项,它必须搜索数组参数一次。 它不读取或打印任何内容

那么我应该首先修正intvalue=0的方式吗?还是我的for循环有问题?或者在第二个循环中,我应该不使用int last作为list.length吗

 public static int indexOf(int[] list, int searchValue) throws BadArrayException 
    {
        int indexValue = 0;

        if(list == null)
            throw new BadArrayException("Array is null");
        else if(list.length == 0)
            return -1;

        for(int i = 0; i < list.length; i++){
            if(list[i] == searchValue)
                indexValue = i;
        }
        return indexValue;
    }

    public static int lastIndexOf(int[] list, int searchValue) throws BadArrayException
    {
        int indexValue = 0;
        int last = list.length;

        if(list.length == 0)
            return -1;

        for(int i = last; i >= 0; i--){
            if(list[i] == searchValue)
                indexValue = i;
        }
        return indexValue;        
    }
}
这是我得到的 ---测试indexOf和lastIndexOf方法---

获取空数组的indexOf OK-indexOf为空数组引发异常:BadArrayException

获取空数组的lastIndexOf 错误-lastIndexOf引发意外异常:java.lang.NullPointerException

获取的索引(5)为:[] OK-预期indexOf返回-1并获得:-1

获取以下项的(5)的最新索引:[] OK-期望lastIndexOf返回-1并获得:-1

获取[20]的(20)个索引 OK-预期indexOf返回0并获得:0

获取[20]的索引(25) 错误-预期indexOf返回-1,但得到:0

获取[20]中的(20)的最新索引 OK-期望lastIndexOf返回0并获得:0

获取[20]中的(25)的最新索引 错误-期望lastIndexOf返回-1,但得到:0

获取[5,10,15,20,10,15,5,10,15,20] 错误-预期indexOf返回0,但得到:6

获取[5,10,15,20,10,15,5,10,15,20]的(10)索引 错误-预期indexOf返回1,但得到:7

获取[5,10,15,20,10,15,5,10,15,20]的(15)索引 错误-预期indexOf返回2,但得到:8

获取[5,10,15,20,10,15,5,10,15,20] 错误-预期indexOf返回3,但得到:9

获取[5,10,15,20,10,15,5,10,15,20]的索引(0) 错误-预期indexOf返回-1,但得到:0

获取[5,10,15,20,10,15,5,10,15,20]中(5)的最新索引 错误-预期lastIndexOf返回6,但得到:0

获取[5,10,15,20,10,15,5,10,15,20]中(10)的最新索引 错误-预期lastIndexOf返回7,但得到:1

获取[5,10,15,20,10,15,5,10,15,20] 错误-预期lastIndexOf返回8,但得到:2

获取[5,10,15,20,10,15,5,10,15,20] 错误-预期lastIndexOf返回9,但得到:3

获取[5,10,15,20,10,15,5,10,15,20] 错误-期望lastIndexOf返回-1,但得到:0


完成-按enter键结束程序

问题在于上次的
初始化

获取
样本的
字符串
“SAMPLE.tocharray().length
的值应该是5,但在循环中,您希望从
“SAMPLE.tocharray()[4]
循环到
“SAMPLE.tocharray()[0]


i、 e.将
last
更改为
“SAMPLE.tocharray()。长度-1
让我们从
索引开始

找到要搜索的数字后,将索引存储在变量中,然后在完成后返回变量。让我们看看
[5,10,5,15,5]
测试中发生了什么:

  • 测试
    列表[0]==5
    。为True,因此
    indexValue=0
理想情况下,此时我们应该返回
0
。但事实并非如此:

  • 测试
    列表[1]==5
    。错,所以什么也不做
  • 测试
    列表[2]==5
    。正确,因此
    indexValue=2
  • 测试
    列表[3]==5
    。错,所以什么也不做
  • 测试
    列表[4]==5
    。正确,因此
    indexValue=4
  • 返回
    4
    。不应该是
    0
事实证明,
indexOf
的工作原理与
lastIndexOf
应该的一样。不要返回上次找到值时的索引,而是在找到值后立即返回索引

lastIndexOf
有同样的问题,但它也有另一个问题,那就是如何初始化
last
。假设我们有一个1元素数组,如下所示:

[1]

此数组的最后一个索引是
0
,但您正在使用
list.length
初始化
last
,它是
1
,而不是
0
。你必须减去1。

问题:for(int i=last;i>=0;i--)应该改为for(int i=last-1;i>=0;i--)索引从0开始,你告诉我们应该发生什么。实际发生了什么?如果您试图找到某个对象的第一个索引,那么当您找到要查找的对象时,请停止查找。说明中没有说明这一点,但看起来您应该在没有找到要查找的值时返回-1。你需要确保你做到了。