Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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_Matrix - Fatal编程技术网

Java中的方法和数组

Java中的方法和数组,java,arrays,matrix,Java,Arrays,Matrix,我是编程新手,我正在努力用一种方法解决一个错误。这是一项作业,我会尽力解释它,有点长 首先 我有一个方法stepMaxDiffint[]数组,它返回一个数组中元素之间最大绝对差的索引。例如: Array = [7, 19, 5, 10, 16, 8, 1, 19, 6, 13] Index = [0, 01, 2, 03, 04, 5, 6, 07, 8, 09] 在这个数组中,最大的绝对差是18=1-19,它发生在索引号6中。此方法在我的代码中正常工作 第二 我有一个选择int[][]数

我是编程新手,我正在努力用一种方法解决一个错误。这是一项作业,我会尽力解释它,有点长

首先

我有一个方法stepMaxDiffint[]数组,它返回一个数组中元素之间最大绝对差的索引。例如:

Array = [7, 19, 5, 10, 16, 8, 1, 19, 6, 13] 
Index = [0, 01, 2, 03, 04, 5, 6, 07, 8, 09] 
在这个数组中,最大的绝对差是18=1-19,它发生在索引号6中。此方法在我的代码中正常工作

第二

我有一个选择int[][]数组的方法,它只需要接收一个矩阵int[]]作为参数,并计算stepMaxDiff找到的数字与矩阵中每一行的长度(数组行的长度)之间的差值。之后,它以较小的距离打印线条。这很简单,我将举一个例子:

          [17, 11, 02]
 Matrix = [19, 02, 18, 12]
          [14, 02]
在此矩阵中,select方法需要返回一条消息,说明第3行是select方法。 之所以会出现这种情况,是因为第3行是stepMaxDiff发现的最大绝对差发生在数组的最后一个元素附近。差异发生在索引0=14-2,第3行只有两个元素索引0和1

第三

最后是我的代码,在那里我疯狂地发现我的错误

    int stock = 1000000; // Absurd value to force the variable difference to be smaller than stock when the first iteration 
    int difIndex = 0; // Index where the biggest difference happens
    int difference = 0; // Calculates the difference between stepMaxDiff and matrix.length
    int line; // Number of the line in matrix

    for (line = 0; line < matrix.length; ++line) { // loop runs all over the lines of the matrix

         Math.abs(difference = stepMaxDiff(matrix[line]) - matrix.length);

         if (difference <= stock) { // If difference is smaller than stock it stores the value of difference
            stock = difference;
            difIndex = line; // Stores the line with the smallest difference
         }

    }

  System.out.print("the line select is " + difIndex); 

我的代码返回第3行,但正确的是第1行。可能是因为它没有在矩阵的行上正确循环,但我不确定。

在第一部分,afterstepMaxDiff中,标记为Index的第二个数组代表什么?它只代表数组中数字的索引。我试图更好地解释是什么stepMaxDiffreturnsI将这些偏移量放进了注释中,添加第二个数组非常令人困惑。另外,在第二部分中,矩阵中每行的长度是多少?您是指数组行的长度吗?读取的字符数?还有什么?我是指数组的长度。我会重读我的帖子,尽量少混淆我的母语不是英语…为什么你要减去这个:'matrix.length-1'而不是行的长度?矩阵[行]。长度?。因为你说的是:stepMaxDiff找到的数字和矩阵中每一行的长度之差
[04, 01, 09, 16, 19, 06, 06, 06, 08, 18]
[15, 12, 18, 06]
[01, 15]
[01, 16, 03, 17, 14, 06]