博客
关于我
Java中二维数组按字典排序(即,按第一列排序)
阅读量:703 次
发布时间:2019-03-21

本文共 2551 字,大约阅读时间需要 8 分钟。

在Java编程中,对二维数组进行排序时,如果需要按照特定列来排序,可以直接使用Arrays.sort()方法并传递自定义的比较器。这将使代码更加简洁高效。

以下是关于如何按照不同列排序二维数组的详细指南:

按列排序通过自定义比较器实现

在Java中,可以通过自定义比较器将二维数组按照指定列排序。以下是一个示例:

import java.util.Arrays;import java.util.Comparator;import java.util.Scanner;public class T_17 {	public static void main(String[] args) {		Scanner sc = new Scanner(System.in);		int n = sc.nextInt();		int[][] a = new int[n][3];		for (int i = 0; i < n; i++) {			a[i][0] = sc.nextInt();			a[i][1] = sc.nextInt();			a[i][2] = sc.nextInt();		}		// 按第一列排序		Arrays.sort(a, new Comparator
() { public int compare(int[] e1, int[] e2) { return Integer.compare(e1[0], e2[0]); } }); // 按第二列排序 Arrays.sort(a, new Comparator
() { public int compare(int[] e1, int[] e2) { return Integer.compare(e1[1], e2[1]); } }); // 按第三列排序 Arrays.sort(a, new Comparator
() { public int compare(int[] e1, int[] e2) { return Integer.compare(e1[2], e2[2]); } }); // 如果需要降序排序,可以通过比较符号调整 Arrays.sort(a, new Comparator
() { public int compare(int[] e1, int[] e2) { return Integer.compare(e2[0], e1[0]); } }); for (int i = 0; i < n; i++) { System.out.println(a[i][0] + " " + a[i][1] + " " + a[i][2]); } }}

关于排序方式的解释

  • 自然排序:上述代码展示了如何按不同列进行排序。第一个Arrays.sort()方法是按第一列升序排序,第二个是按第二列升序排序,第三个是按第三列升序排序。

  • 降序排序:如果需要按相反的排序顺序(降序),可以在比较方法中调换Integer.compare()的参数,例如:

  • Arrays.sort(a, new Comparator
    () { public int compare(int[] e1, int[] e2) { return Integer.compare(e2[0], e1[0]); }});

    这种方法简单且高效,能够满足大多数实际需求。

    替代方案:转换为字符串数组

    除了使用自定义比较器,你也可以通过将二维数组转换为字符串数组来排序。这种方法虽然略微麻烦,但在某些特定场景下可能更有用。

    转换为字符串数组的步骤:

  • 定义一个辅助方法,将二维数组转换为字符串数组:
  • public static String[][] intArrayToStringArray(int[][] array) {	String[][] result = new String[array.length][];	for (int i = 0; i < array.length; i++) {		result[i] = IRSUtils.arrayToString(array[i]);	}	return result;}
    1. 实现arrayToString方法:
    2. private static String arrayToString(int[] array) {	return Arrays.toString(array);}
      1. 进行排序:
      2. String[][] primaryStage = intArrayToStringArray(a);Arrays.sort(primedStage);String[][] sortedArray = intArrayToStringArray(primedStage);
        1. 将排序后的字符串数组转换回整数数组:
        2. int[][] sortedIntArray = new int[sortedArray.length][];for (int i = 0; i < sortedArray.length; i++) {	String[] values = sortedArray[i];	sortedIntArray[i][0] = Integer.parseInt(values[0]);	sortedIntArray[i][1] = Integer.parseInt(values[1]);	sortedIntArray[i][2] = Integer.parseInt(values[2]);}

          这种方法虽然可行,但比直接使用自定义比较器要复杂许多,代码量也更大。

          总结

          在Java中,选择何种排序方法取决于具体需求。如果你希望代码简洁且高效,建议直接使用自定义比较器来对二维数组按照指定列排序。如果需要额外的灵活性,可以考虑通过转换到字符串数组的方式,但要权衡代码复杂度。

    转载地址:http://edtez.baihongyu.com/

    你可能感兴趣的文章
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.7 Parameters vs Hyperparameters
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named 'pandads'
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>