赞
踩
首先定义一个5X8的二维数组,然后使用随机数填充满。
借助Arrays的方法对二维数组进行排序。
先把二维数组使用System.arraycopy进行数组复制到一个一维数组
然后使用sort进行排序
最后再复制回到二维数组。
- package Test;
- import java.lang.Math;
- import java.util.Arrays;
- public class ArraysClassLearning {
- public static void main(String[] args) {
- int [][] a = new int[5][8];
- int [] b = new int[40];
- for(int i=0;i<5;i++) {
- for(int j=0;j<8;j++) {
- a[i][j] = (int)(Math.random()*100);
- // System.out.print(a[i][j]+" ");
- }
- System.arraycopy(a[i], 0, b, i*8, 8);
- }
- System.out.println("二维数组为:");
- for(int[] i:a) {
- for(int j:i) {
- System.out.print(j+" ");
- }
- System.out.println();
- }
- Arrays.sort(b);
- for(int i:b) {
- System.out.print(i+" ");
- }
- System.out.println();
- for(int i=0;i<5;i++) {
- a[i] = Arrays.copyOfRange(b, i*8, i*8+8);
- }
- System.out.println("排序后二维数组为:");
- for(int[] i:a) {
- for(int j:i) {
- System.out.print(j+" ");
- }
- System.out.println();
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。