[英]Sorting Two-Dimensional Array by Row
The requirement is to sort the rows of a two-dimensional array. 要求是对二维数组的行进行排序。 I feel like my code is very close to being done, but I can't figure out why it isn't displaying the sorted array.
我觉得我的代码非常接近完成,但我无法弄清楚为什么它没有显示已排序的数组。 I forgot to mention that we are not allowed to use the premade sorting methods.
我忘了提到我们不允许使用预制的排序方法。 The problem is most likely in the sortRows method.
问题很可能出在sortRows方法中。 Anyways, here's my code:
无论如何,这是我的代码:
public class RowSorting
{
public static void main(String[] args)
{
double[][] numbers = new double[3][3];
double[][] number = new double[3][3];
int run = 0;
String answer = "";
while (run == 0)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a 3-by-3 matrix row by row: ");
for(int row = 0; row < numbers.length; row++)
{
for(int column = 0; column < numbers[row].length; column++)
{
numbers[row][column] = input.nextDouble();
}
}
for(int row = 0; row < numbers.length; row++)
{
for(int column = 0; column < numbers[row].length; column++)
{
System.out.print(numbers[row][column] + " ");
}
System.out.print("\n");
}
System.out.println("The sorted array is: \n");
number = sortRows(numbers);
for(int row = 0; row < number.length; row++)
{
for(int column = 0; column < number[row].length; column++)
{
System.out.print(number[row][column] + " ");
}
System.out.print("\n");
}
System.out.print("\nWould you like to continue the program (y for yes or anything else exits): ");
answer = input.next();
if(answer.equals("y"))
{
continue;
}
else
break;
}
}
public static double[][] sortRows(double[][] m)
{
for(int j = 0; j < m[j].length - 1; j++)
{
for(int i = 0; i < m.length; i++)
{
double currentMin = m[j][i];
int currentMinIndex = i;
for(int k = i + 1; k < m[j].length; k++)
{
if(currentMin > m[j][i])
{
currentMin = m[j][i];
currentMinIndex = k;
}
}
if(currentMinIndex != i)
{
m[currentMinIndex][j] = m[j][i];
m[j][i] = currentMin;
}
}
}
return m;
}
}
It looks like this block: 它看起来像这个块:
if(currentMin > m[j][i])
{
currentMin = m[j][i];
currentMinIndex = k;
}
Will never happen. 永远不会发生。 Because you just assigned currentMin to m[j][i] two lines before it.
因为你刚才将currentMin分配给m [j] [i]前面的两行。 I believe you want to use k in that if check.
如果检查,我相信你想用k。 Something like
就像是
if (currentMin > m[j][k]){
currentMin = m[j][k];
currentMinIndex = k;
}
As cited per ergonaut, you have a problem with the codeblock 正如ergonaut所引用的,您遇到了代码块问题
if(currentMin > m[j][i]) ...
and also 并且
m[currentMinIndex][j] = m[j][i];
However, you also have a problem with your for-loops. 但是,你的for循环也有问题。
for(int j = 0; j < m[j].length - 1; j++) ...
for(int i = 0; i < m.length; i++) ...
Both of these are oddly structured. 这两者都是奇怪的结构。 You probably want to swap these for-loops so that you don't throw index exceptions.
您可能希望交换这些for循环,以便不抛出索引异常。 This will also cause you address the indices in your code.
这也会导致您在代码中处理索引。 And modify your j-index for-loop to include the entire range.
并修改你的j-index for-loop以包含整个范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.