Java 对2D数组按列排序

Java 对2D数组按列排序

在数据结构领域中,向量是一个特定对象的可增长类数组。向量类属于遗留类,与集合完全兼容。在java.util包中,列表接口可以使用此处列出的所有方法。初始容量为10,通用方法为:

Vector<E> v = new Vector<E>();

compare()方法接受两个参数,然后使用Java环境逻辑进行比较。

今天在这篇文章中,我们将学习如何对2D数组的列进行排序。

按列对2D数组进行排序的算法

以下是对2D数组按列进行排序的特定算法。

  • 步骤1 – 开始。

  • 步骤2 – 逐个遍历所有列。

  • 步骤3 – 在该列中添加元素到向量中。

  • 步骤4 – 处理这些向量。

  • 步骤5 – 再次对它们进行排序。

  • 步骤6 – 将它们从向量推回到列中。

  • 步骤7 – 删除所有这些向量,使集合为空。

  • 步骤8 – 重新开始排序。

  • 步骤9 – 重复所有步骤。

  • 步骤10 – 一步一步地完成所有的列。

  • 步骤11 – 结束过程。

按列对2D数组进行排序的语法

下面是按列对一些2D数组进行排序的特定语法:

A. removeAll():

语法:

Vector.removeAll(Vectors As Value)
It is used to remove all the elements from the vector.

B. Collections.sort():

语法:

Collections.sort(Vectors As Value)
This method is used to sort the vector in a process.

C. add():

语法:

Vector.add(Value as the integer value)
It is used to add some elements in the vector.

D. get():

语法:

Vector.get(3);
This method used to store the vector element at a pricular index.

在这些特定的语法中,我们尝试对某些2D数组按列排序。

对2D数组按列排序的方法

  • 方法1 – Java程序,按列排序2D数组

  • 方法2 – 使用函数Arrays.sort对2D矩阵进行排序的Java代码

  • 方法3 – Java程序,使用冒泡排序将2D数组按照与大小为n * m的1D数组相同的方式排序

  • 方法4 – Java程序,根据第3列排序,比较这些值并按升序更改2D数组的特定顺序

方法1:Java程序,按列排序2D数组

在这个Java代码中,我们尝试以一般方式对2D数组按列进行排序。

示例1

import java.io.*;
import java.lang.*;
import java.util.*;

public class ARBRDD {
    public static void main(String[] args)
      throws java.lang.Exception{
      int[][] arr = { { 7, 16, 10, 97, 1 },
         { 3, 8, 2, 9, 14 },
         { 5, 1, 0, 5, 2 },
         { 4, 2, 6, 0, 1 } };
      System.out.println("Matrix without sorting here ----> \n");
      for (int i = 0; i < 4; i++) {
         for (int j = 0; j < 5; j++) {
            System.out.print(arr[i][j] + " ");
         }
         System.out.println();
      }
      Vector<Integer> v = new Vector<>();

      for (int i = 0; i < 5; i++) {
         for (int j = 0; j < 4; j++) {
            v.add(arr[j][i]);
        }
        Collections.sort(v);

        for (int j = 0; j < 4; j++) {
           arr[j][i] = v.get(j);
        }
        v.removeAll(v);
      }

      System.out.println("Matrix after sorting is ----> \n");

      for (int i = 0; i < 4; i++) {
         for (int j = 0; j < 5; j++) {
            System.out.print(arr[i][j] + " ");
      }

      System.out.println();
      }
   }
}

输出

Matrix without sorting here ----> 

7 16 10 97 1 
3 8 2 9 14 
5 1 0 5 2 
4 2 6 0 1 
Matrix after sorting is ----> 

3 1 0 0 1 
4 2 2 5 1 
5 8 6 9 2 
7 16 10 97 14

方法2:使用函数Arrays.sort的Java代码对2D矩阵进行排序

在这个Java代码中,我们尝试展示了如何通过使用Arrays.sort方法以一般的方式对2D数组的列进行排序。

示例2

import java.util.*;
public class sort2DMatrixbycolumn2022 {
   public static void sortbyColumn(int arr[][], int col){
      Arrays.sort(arr, new Comparator<int[]>() {

         @Override          
         public int compare(final int[] entry1, final int[] entry2) {
            if (entry1[col] > entry2[col])
               return 1;
            else
               return -1;
         }
      }); 
   }
   public static void main(String args[]){
      int matrix[][] = { { 39, 27, 11, 42 },
         { 10, 93, 91, 90 },
         { 54, 78, 56, 89 },
         { 24, 64, 20, 65 } };
      int col = 3;
      sortbyColumn(matrix, col - 1);

      for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix[i].length; j++)
           System.out.print(matrix[i][j] + " ");
        System.out.println();
      }
    }
}

输出

39 27 11 42 
24 64 20 65 
54 78 56 89 
10 93 91 90

方法3:Java程序以与大小为n * m的1D数组相同的方式排序2D数组,使用冒泡排序

在这个Java代码中,我们试图展示如何以通用的方式对2D数组进行排序,以与大小为n * m的1D数组相同的方式,跨列进行排序。

示例3

package jex;
import java.util.*;
public class demo {
   public static void sort(int arr[][]) {
      int i, j, temp;
      int n=arr.length;
      int m=arr[0].length;
      for (i = 0; i < n * m - 1; ++i) {
         for (j = 0; j < n * m - 1 - i; ++j) {
            if (arr[j / m][j % m] > arr[(j + 1) / m][(j + 1) % m]) {
               temp = arr[(j + 1) / m][(j + 1) % m];
               arr[(j + 1) / m][(j + 1) % m] = arr[j / m][j % m];
               arr[j / m][j % m] = temp;
            }
         }
      }
   }
   public static void print(int arr[][]) {
      int i, j;
      int n=arr.length;
      int m=arr[0].length;
      for (i = 0; i < n; ++i) {
         for (j = 0; j < m; ++j) {
            System.out.print(arr[i][j]+" ");
         }
         System.out.println();
      }
   }
   public static void main(String[] args){
      Scanner sc=new Scanner(System.in);
      int[][] arr={ { 5, 12, 17, 12, 23},
         { 1, 2, 4, 6, 8},
         {21, 14, 7, 19, 27},
         { 3, 18, 9, 15, 25}
      };
      System.out.println("Array Before Sorting is here ---->: ");
      print(arr);
      sort(arr);
      System.out.println("Array After Sorting is here ----> : ");
      print(arr);
   }
}

输出

Array Before Sorting is here ---->: 
5 12 17 12 23 
1 2 4 6 8 
21 14 7 19 27 
3 18 9 15 25 
Array After Sorting is here ----> : 
1 2 3 4 5 
6 7 8 9 12 
12 14 15 17 18 
19 21 23 25 27

方法4:根据3列的值进行排序的Java程序,根据这些值进行比较,并将2D数组的特定顺序按升序进行更改

在这个Java代码中,我们尝试展示如何根据3列的值进行排序,根据这些值进行比较,并按升序更改2D数组的特定顺序。

示例4

import java.util.Arrays;
import java.util.Comparator;

public class Sort2DArray {
   public static void main(String args[]) {
      int[][] multi = new int [][]{
         {4, 9, 8},
         {7, 5, 2},
         {3, 0, 6},

      };
      for(int i = 0; i< multi.length; i++) {
         for (int j = 0; j < multi[i].length; j++)
         System.out.print(multi[i][j] + " ");
         System.out.println();

      }
      Sort2DArrayBasedOnColumnNumber(multi,3);
      System.out.println("after sorting we get some value ---->");
      for(int i = 0; i< multi.length; i++) {
         for (int j = 0; j < multi[i].length; j++)
         System.out.print(multi[i][j] + " ");
         System.out.println();

      }
   }
    public static  void Sort2DArrayBasedOnColumnNumber (int[][] array, final int columnNumber){
      Arrays.sort(array, new Comparator<int[]>() {
         @Override
         public int compare(int[] first, int[] second) {
            if(first[columnNumber-1] > second[columnNumber-1]) return 1;
            else return -1;
         }
      });
   }
}

输出

4 9 8 
7 5 2 
3 0 6 
after sorting we get some value ---->
7 5 2 
3 0 6 
4 9 8

结论

从上面的讨论中,我们详细了解了2D数组问题的排序。今天,我们使用了不同的排序方法来通过上述的语法和算法解决这个问题。希望通过本文,您已经对如何使用Java环境对2D数组问题进行排序有了广泛的了解。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程