【问题标题】:Java sort second array depending on indexes of first arrayJava根据第一个数组的索引对第二个数组进行排序
【发布时间】:2017-09-28 07:22:30
【问题描述】:

我有两个 int 数组

int[] sum=new int[n];
int[] newTime=new int[n];
  1. 第一:1 5 3
  2. 第二个 10 15 13

Arrays.sort(sum);

  1. 打印 1 3 5

我想要的甚至是使用相同的索引对第二个数组进行排序 的

  1. 第一=>第二:10 13 15

我尝试过使用地图:

SortedMap<Integer, Integer> m = new TreeMap<Integer, Integer>();
        for(int i = 0; i < priorities.length; i++){
            m.put(sum[i],newTime[i]);
        }

它只对第一个数组进行排序,第二个数组的索引不会改变。感谢帮助! 谢谢!

【问题讨论】:

  • 你为什么不使用Arrays.sort(newTime); ?我再一次不明白你想要达到什么目的。用例是什么?
  • @Eran 将其作为答案发布,以便我检查!
  • @ShubhenduPramanik 因为第一个数组可以是 1 7 9 和第二个 20 11 3

标签: java arrays sorting maps


【解决方案1】:

你可以像这样使用 java-8 来做到这一点:

    int[] left = new int[] { 1, 5, 3 };
    int[] right = new int[] { 10, 15, 13 };

    IntStream.range(0, left.length)
            .boxed()
            .map(x -> new AbstractMap.SimpleEntry<>(left[x], right[x]))
            .sorted(Comparator.comparing(SimpleEntry::getKey))
            .forEach(System.out::println);

编辑

实际得到第二个数组:

Integer[] second = IntStream.range(0, left.length)
            .boxed()
            .map(x -> new AbstractMap.SimpleEntry<>(left[x], right[x]))
            .sorted(Comparator.comparing(SimpleEntry::getKey))
            .map(SimpleEntry::getValue)
            .toArray(Integer[]::new);

【讨论】:

  • 我只有一个问题,现在如何获得第二个数组?如果我执行 String a= Arrays.toString(right); System.out.println(a);它打印第二个数组但它没有排序我怎么能得到它?
  • 再次超级棒!非常感谢简单快速
  • 先生,我有两个不同类型的 ArrayList。 ArrayList 是第一个arraylist ArrayList 是第二个arraylist 在我的情况下可以做同样的事情吗?我正在尝试,但它在 DoubleStream 上给了我错误..
  • @QaisarKhanBangash 发布一个单独的问题,这就是这个网站的用途。
【解决方案2】:

您的TreeMap 方法可以满足您的需求:

SortedMap<Integer, Integer> m = new TreeMap<Integer, Integer>();
for(int i = 0; i < priorities.length; i++){
    m.put(sum[i],newTime[i]);
}
// this will print the elements of the second array in the required order
for (Integer i : m.values()) {
    System.out.println (i);
}

当然,如果需要,您可以将元素分配回原始数组:

int count = 0;
for (Integer i : m.values()) {
    newTime[count] = i;
}

正如 mlecz 正确评论的那样,此解决方案仅在第一个数组 (sum) 没有重复项时才有效。

【讨论】:

  • 第一个数组有重复时是否正确?
  • @mlecz 不,它不适用于第一个数组中的重复项
  • 放第一个数组 :1 5 3 放第二个 10 15 13 [1=10, 3=15, 5=13] 这就是它打印出来的我需要的是 1=10 3=13 5=15
  • @E99 根据您在问题中调用 put (1,10)、put(5,15) 和 put(3,13) 的代码,因此地图不能包含 3=15 5=13。也许您的意见与您想象的不同。
  • 我先放了第一个数组,然后放了第二个,所以首先我也可以放 2 9 3 和第二个 20 50 90。输出应该是:2=20 3=90 9=50 这是什么我正在尝试根据第一个数组索引对第二个进行排序
【解决方案3】:

这是解决方案,当第一个数组中有重复项时有效。 您将两个数组中的值保存在一起,使用一个数组中的索引对它们进行排序,然后根据第二个数组中的相应索引创建一个列表。

  static Integer[] sort(int[] arr1, int[] arr2) {
    assert arr1.length == arr2.length;
    class Tuple{
      int a1;
      int a2;
    }

    List<Tuple> tuples = new ArrayList<>();
    for(int i=0;i<arr1.length;i++){
      Tuple t = new Tuple();
      t.a1=arr1[i];
      t.a2=arr2[i];
      tuples.add(t);
    }
    tuples.sort((t1,t2)->Integer.compare(t1.a1,t2.a1));

    return (Integer[]) tuples.stream().map(t->t.a2).collect(Collectors.toList()).toArray(new Integer[arr1.length]);
  }

【讨论】:

    【解决方案4】:

    您可以同时对两个数组进行排序,如下所示对第一个数组进行重新排序:

    int[] sum = { 1, 5, 3 };
    int[] newTime = { 10, 15, 13 };
    
    for (int i = 0; i < sum.length; i++) {
        for (int j = 0; j < sum.length; j++) {
            if (sum[i] < sum[j]) {
    
                int temp = sum[i];
                int temp2 = newTime[i];
    
                sum[i] = sum[j];
                sum[j] = temp;
    
                newTime[i] = newTime[j];
                newTime[j] = temp2;
            }
        }
    }
    
    System.out.println(Arrays.toString(sum));
    System.out.println(Arrays.toString(newTime));
    

    【讨论】:

      猜你喜欢
      • 2020-11-19
      • 2018-11-18
      • 2018-05-17
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      • 2023-01-31
      • 1970-01-01
      • 2013-08-18
      相关资源
      最近更新 更多