排序阵列和反映另一个数组的变化 [英] Sort array and reflect the changes in another array

查看:138
本文介绍了排序阵列和反映另一个数组的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有双打的数组,在Java中: ARR1 我要排序。最有可能第一个选项将是实用方法 Arrays.sort(双[])

I have an array of doubles, in Java : arr1 which I want to sort. Most probably the first option would be the utility method Arrays.sort(double[]).

的想法是,我想同样的变化(在如价值指数I 指标j互换值为 ARR1 ),以反映在另一个整数数组: ARR2 (在这个意义上,在相同的价值观指数也改变了 ARR2 )。

The idea is that I want the same changes (e.g. value at index i is interchanged with value at index j in arr1) to be reflected in another array of integers: arr2 (in the sense that the values at the same indexes are changed also in arr2).

有没有一种简单的方法(一招)在Java中做到这一点?或者,唯一的办法就是由我自己来实现排序算法?

Is there a simple way (a trick) to accomplish this in Java? Or the only way is to implement the sorting algorithm by myself?

更新:我看到人建议从 ARR1 包含两个值(一个对象的一个​​数组,一名来自更换两个数组 ARR2 )。这是不是带来一定的效率损失。换句话说,是不是效率较低排序的对象比原始类型的数组(在这种情况下,双打)?

UPDATE: I see that people recommend replacing the two arrays with one array of objects containing the 2 values (one from arr1 and one from arr2). Wouldn't this bring some efficiency penalties. In other words, isn't it less efficient to sort an array of objects than an array of primitive types (doubles in this case) ?

中的数据是完全静态的。这是大的(它适合在内存中),但静态的。

The data is completely static. It's large (it fits in memory) but static.

推荐答案

而不是试图维持有序并行阵列,更清洁的解决方案是创建一个封装了数据值的一类,只是有一个对象数组

Rather than trying to maintain sorted parallel arrays, a cleaner solution would be to create a class that encapsulates both of your data values, and just have one array of objects.

(但是,为了回答你的问题,没有内置的方式在Java中做到这一点。实现自己的排序例程使排序的两个阵列基于价值观在其中的一个会为ISN小数据量的工作'吨可能改变,但它会难以维护。)

(But to answer your question, there is no built-in way to do this in Java. Implementing your own sort routine that keeps two arrays sorted based on values in one of them would work for a small amount of data that isn't likely to change, but it would be difficult to maintain.)

这篇关于排序阵列和反映另一个数组的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆