如何按升序和降序对2维整数数组进行排序

时间:2015-09-18 08:01:46

标签: javascript

我是java技术的新手,我遇到了处理2D arrays的问题。我尝试了很多逻辑,都是基于column或任何其他标准。我有一个案例要在我的项目中解决这个问题

我有一个a[3][3]矩阵 喜欢

9 8 7
4 3 2
0 5 4

我的标准是我想要矩阵

0 2 3
4 4 5
7 8 9

以及

9 8 7  
5 4 4
3 2 0

我的Java代码

 public class MatrixExample {

     Scanner scan;

     Integer matrix[][];

     int row,column;

 void create() {

     scan=new Scanner(System.in);

     System.out.println("Matrix creation");

     System.out.println("\n Enter Rows");

     row=Integer.parseInt(scan.nextLine());

     System.out.println("\n Enetr columns");

     column=Integer.parseInt(scan.nextLine());

     matrix=new Integer[row][column];

     System.out.println("Enter the data");


     for(int i=0;i<row;i++) {

        for(int j=0;j<column;j++) {

            matrix[i][j]=scan.nextInt();

        }

    }

}

void display() {

    for(int i=0;i<row;i++) {

        for(int j=0;j<column;j++) {

            System.out.print("\t" +matrix[i][j]);

        }



        System.out.println();

    }

}
void sortmatrix() {
     Arrays.sort(matrix, new Comparator<Integer[]>(){
            @Override
            public int compare(Integer[] o1, Integer[] o2) {
                return o1[0].compareTo(o2[0]);
            } });
    System.out.println("After:");
    for(Integer[] row : matrix) {
        Arrays.sort(row);
        for(Integer num : row) {
            System.out.print(num);
        }
        System.out.println("");
    }

}
public static void main(String []args){
    MatrixExample h= new MatrixExample();
    h.create();
    h.display();
    h.sortmatrix();
 }
}

3 个答案:

答案 0 :(得分:0)

从概念上讲,你没有2D 3x3矩阵,当你按行排序时,你有一个9个值的线性数组。

要获得所需的结果,请输入9个值,逐渐对它们进行排序并以两种方式输出:

  • 作为3x3阵列,
  • 作为3x3数组,以相反的顺序获取数组元素。

答案 1 :(得分:0)

有些代码会对您有所帮助:

  private static void sortmatrix() {
      int[] elements = new int[row*column];
      int count=0;
      //get elements from matrix
       for(int i=0;i<row;i++) {
            for(int j=0;j<column;j++,++count) {
                elements[count]=matrix[i][j];
            }
        }

       System.out.println(Arrays.toString(elements));
       //sort them
       Arrays.sort(elements);
       System.out.println(Arrays.toString(elements));
       //set the numbers in the matrix, ordered
       count=0;
       for(int i=0;i<row;i++) {
            for(int j=0;j<column;j++,++count) {
                matrix[i][j]=elements[count];
        }

    }

如果您希望矩阵按照后代顺序,只需使用以下命令创建矩阵:

 count=elements.lenght-1;
 for(int i=0;i<row;i++) {
        for(int j=0;j<column;j++,--count) {
           matrix[i][j]=elements[count];
  }

答案 2 :(得分:0)

我认为你实际上并没有尝试对2D数组进行排序。您有其他一些具体标准。无论如何,我将按升序为您提供2D数组排序:

    public class ArraySorting {

  /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Krishna
 */

        public static void main(String args[])throws IOException
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

            System.out.print("Enter the no. of  rows: "); //inputting number of rows
            int m=Integer.parseInt(br.readLine());
            System.out.print("Enter the no. of columns: "); //inputting number of columns
            int n=Integer.parseInt(br.readLine());

            int A[][]=new int[m][n]; //creating a 2D array

            /* Inputting the 2D Array */

            System.out.print("Enter the elements: ");
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<n;j++)
                {

                    A[i][j]=Integer.parseInt(br.readLine());
                }
            }        

            /* Printing the original 2D Array */

            System.out.println("The original array:");
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<n;j++)
                {
                    System.out.print(A[i][j]+"\t");
                }
                System.out.println();
            }

            /* Sorting the 2D Array */

            int t=0;
            for(int x=0;x<m;x++)
            {
                for(int y=0;y<n;y++)
                {
                    for(int i=0;i<m;i++)
                    {
                        for(int j=0;j<n;j++)
                        {
                            if(A[i][j]>A[x][y])
                            {
                                t=A[x][y];
                                A[x][y]=A[i][j];
                                A[i][j]=t;
                            }
                        }
                    }
                }
            }

            /* Printing the sorted 2D Array */

            System.out.println("The Sorted Array:");
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<n;j++)
                {
                    System.out.print(A[i][j]+"\t");
                }
                System.out.println();
            }
        }
    }

以上代码的输出如下:

Output

还有其他方法可以对2D数组进行排序。

相关问题