Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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 数组方法打印几乎是正确的,只有一个值是关闭的,我不知道为什么_Java_Arrays_Loops_For Loop_Output - Fatal编程技术网

Java 数组方法打印几乎是正确的,只有一个值是关闭的,我不知道为什么

Java 数组方法打印几乎是正确的,只有一个值是关闭的,我不知道为什么,java,arrays,loops,for-loop,output,Java,Arrays,Loops,For Loop,Output,因此,我试图将一个数组传递到一个单独的方法中,然后返回一个新大小的数组,但程序只有一个不正确的值。例如,我有一个数组 int [] myInches = {89,12,33,7,72,42,76,49,69,85,61,23}; 我正试图传递到我的createLowerArray方法中 public static int [] createLowerArray(int maxParam, int [] myInchesParam) { int [] betterInches = {0

因此,我试图将一个数组传递到一个单独的方法中,然后返回一个新大小的数组,但程序只有一个不正确的值。例如,我有一个数组

int [] myInches = {89,12,33,7,72,42,76,49,69,85,61,23};
我正试图传递到我的createLowerArray方法中

 public static int [] createLowerArray(int maxParam, int [] myInchesParam) {
    int [] betterInches = {0,0,0,0,0,0,0,0,0,0};
    int count = 0;
    for (int i = 0; i < myInchesParam.length; i++) {
        if (myInchesParam[i] < maxParam) {
            count++;
        }
        betterInches = new int [count];
        int newCount = 0;
        for (int q = 0; q < betterInches.length; q++) {
            if (myInchesParam[newCount] < maxParam) {
                betterInches[q] = myInchesParam[newCount];
            }
            newCount++;
        }
    }
    return betterInches;
}
电流输出为

int length = 4
[0] = 0
[1] = 12
[2] = 33
[3] = 7
更新

实际上,您并不是为了获取参数而对整个数组进行迭代。将第二个循环更改为此

 int newCount = 0;
 for (int q = 0; q < myInchesParam.length; q++) {
     if (myInchesParam[q] < maxParam) {
         betterInches[newCount] = myInchesParam[q];
         newCount++;
     }

 }
分离第一个循环和第二个循环。第二个循环在第一个循环内。看看下面的代码

public static int [] createLowerArray(int maxParam, int [] myInchesParam) {
    int [] betterInches = {0,0,0,0,0,0,0,0,0,0};// Dont need to initialize
    int count = 0;
    for (int i = 0; i < myInchesParam.length; i++) {
        if (myInchesParam[i] < maxParam) {
            count++;
        }
    } // First loop end here

    betterInches = new int [count];
    int newCount = 0;
    for (int q = 0; q < myInchesParam.length; q++) {
       if (myInchesParam[q] < maxParam) {
          betterInches[newCount] = myInchesParam[q];
          newCount++;
       }

    } // Second Loop ends heer

    return betterInches;
}

眼前的问题是:

    for (int q = 0; q < betterInches.length; q++) {
        if (myInchesParam[newCount] < maxParam) {
            betterInches[q] = myInchesParam[newCount];
        }
        newCount++;
    }
此外,您所做的工作远远超出了必要的范围—您当前的代码是输入数组大小的二次方。在外循环的每次迭代中创建新的Betterniches数组,然后丢弃该数组,并在下一次迭代中再次创建它

将内环移出外环:

for (int i = 0; i < myInchesParam.length; i++) {
    if (myInchesParam[i] < maxParam) {
        count++;
    }
}

betterInches = new int [count];
for (int i = 0, q = 0; i < myInchesParam.length; i++) {
    if (myInchesParam[i] < maxParam) {
        betterInches[q++] = myInchesParam[i];
    }
}
试试这个

for (int q = 0; q < betterInches.length; q++) {
        if (myInchesParam[q] < maxParam) {
            betterInches[newCount] = myInchesParam[q];
            newCount++;
           }      
    }

请您只显示预期的和实际的输出,而不是试图描述它?顺便说一下,我怀疑您是否真的想在每个循环迭代中创建一个新的Betterniches:循环中的所有内容,从Betterniches=new int[count];向下应该在循环之外。而newCount++应该在if内部。刚刚用预期和当前输出编辑了这个问题,我真的要做更多对不起。谢谢你,但出于某种原因,当我这样做的时候,它会将所有定位元素的值都变成0。我把newCount++放进去;现在在if循环中,由于某种原因,我的所有元素位置都是0。再次感谢您,但现在它给了我一个ArrayIndexOutOfBoundsBetterniches[newCount]=myInchesParam[q]上的ArrayIndexOfBoundsException错误;线我不知道为什么。
for (int i = 0; i < myInchesParam.length; i++) {
    if (myInchesParam[i] < maxParam) {
        count++;
    }
}

betterInches = new int [count];
for (int i = 0, q = 0; i < myInchesParam.length; i++) {
    if (myInchesParam[i] < maxParam) {
        betterInches[q++] = myInchesParam[i];
    }
}
for (int q = 0; q < betterInches.length; q++) {
        if (myInchesParam[q] < maxParam) {
            betterInches[newCount] = myInchesParam[q];
            newCount++;
           }      
    }