阅读量:115
在Java中,可以使用Arrays类的sort方法对二维数组进行排序。首先,需要将二维数组转换为一维数组,然后再对一维数组进行排序。最后,再将排序后的一维数组转换回二维数组。
以下是一个示例代码:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[][] arr = { { 5, 7, 1 }, { 8, 4, 6 }, { 3, 9, 2 } };
int rows = arr.length;
int cols = arr[0].length;
int[] temp = new int[rows * cols];
int index = 0;
for (int i = 0; i < rows class="hljs-keyword">for (int j = 0; j < cols xss=removed xss=removed class="hljs-number">0;
for (int i = 0; i < rows class="hljs-keyword">for (int j = 0; j < cols xss=removed class="hljs-keyword">for (int i = 0; i < rows class="hljs-keyword">for (int j = 0; j < cols class="hljs-string">" ");
}
System.out.println();
}
}
}
运行上述代码,将输出排序后的二维数组。