二维数组来最高数字排序由最低 [英] 2d Array sorting by lowest to highest number
问题描述
我想这个二维数组进行排序,因此将输出是这样的:
1 2 3
4 5 6
7 8 9
和还告诉我有多少移动时,它作出rearange。谢谢配发!
下面是我的code到目前为止:
公共静态无效的主要(字串[] args){
INT firstArray [] [] = {{3,8,5},{1,6,9},{2,4,7}};
的System.out.println(这是数组排序:);
displayArray(firstArray);
}
公共静态无效displayArray(INT X [] []){
对于(INT行= 0;行< x.length;排++){
对于(INT列= 0;柱< X [行] .length;列++){
System.out.print(X [行] [列] +\ t的);
}
的System.out.println();
}
}
首先,你取出阵列的每一个数字,然后你做一个简单的排序,以找出其中打印数字的顺序。你应该看看了比
方法的Java或使自己的,如果你需要。
当你得到了增加一个整数计算你每一个操作和1每次你做的手术时间增加了。然后打印,当您完成。
I would like to sort this 2D array so it would output something like this:
1 2 3
4 5 6
7 8 9
and also tell me how many moves it made to rearange. Thank you allot !
Here is my code so far:
public static void main(String[] args) {
int firstArray [][] ={{3,8,5},{1,6,9},{2,4,7}};
System.out.println("This is array to sort:");
displayArray(firstArray);
}
public static void displayArray(int x[][]) {
for (int row=0;row<x.length;row++) {
for(int column = 0;column<x[row].length; column++) {
System.out.print(x[row][column]+"\t");
}
System.out.println();
}
}
First you take every single number out of the array, and then you do a simple sort to figure out the order in which to print the numbers. You should look up the compare
method in Java or make your own if you need to.
When you got that add an integer that counts every operation you do and increment it by 1 every time you do an operation. Then print it when you are done.
这篇关于二维数组来最高数字排序由最低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!