英文:
How to sort two arrays with one being sorted based on the sorting of the other?
问题
I understand your request. Here's the translated code part:
所以我遇到过这个问题很多次。让我解释一下。假设我有这两个数组:
A1={1,2,3,4,5,6,7,8,9,10};
和
A2={1,2,3,0,2,1,1,0,0,0};
. 我需要的是:
当我对A2进行排序时,A2中元素的交换和移动操作应该在A1中也同步进行。基本上,我试图使用两个数组来创建一个映射,而不是创建一个实际的HashMap或HashTable。
最终,这两个数组应该如下所示:
A1={4,8,9,10,1,6,7,2,5,3};
和 A2={0,0,0,0,1,1,1,2,2,3};
. 两个数组的对应值仍然相同,但根据A2进行了排序。我需要一种以最快的方式进行这种排序的方法。
有关此问题的建议吗?
英文:
So I have encountered this problem a lot of times. Let me explain. Say I have these two arrays:
A1={1,2,3,4,5,6,7,8,9,10};
and
A2={1,2,3,0,2,1,1,0,0,0};
. What I require is this:
When I sort A2, whatever swapping and shifting of elements takes place in A2, the same should take place in A1 as well. Basically I am trying to create a Map using two arrays instead of creating an actual HashMap or HashTable.
Finally the arrays should look like this:
A1={4,8,9,10,1,6,7,2,5,3};
and A2={0,0,0,0,1,1,1,2,2,3};
. The corresponding values of both arrays are still the same but the data is sorted based on A2. I need a way to do this kind of sort in the fastest way possible.
Any suggestions for this?
答案1
得分: 2
Pair类可以在这里起作用。
import java.util.*;
public class Main
{
static class Pair implements Comparable<Pair>
{
int a1;
int a2;
Pair (int a1, int a2) //构造函数
{
this.a1 = a1;
this.a2 = a2;
}
public int compareTo(Pair other) //使其仅比较a2的值
{
return this.a2 - other.a2;
}
}
public static void main(String[] args)
{
int[] A1 = {1,2,3,4,5,6,7,8,9,10};
int[] A2 = {1,2,3,0,2,1,1,0,0,0};
Pair[] pairs = new Pair[A1.length];
for (int i = 0; i < pairs.length; i++)
{
pairs[i] = new Pair(A1[i], A2[i]);
}
Arrays.sort(pairs);
//打印值
for (int i = 0; i < A1.length; i++)
{
System.out.print(pairs[i].a1 + " ");
}
System.out.println();
for (int i = 0; i < A2.length; i++)
{
System.out.print(pairs[i].a2 + " ");
}
}
}
通过创建一个Pair类来保存两个变量a1
和a2
,您可以重写compareTo
方法,只比较a2
的值,这样当调用Arrays.sort
时,Pair数组中的元素将仅根据a2
的值进行交换。然后,您可以访问这些Pair中的值并打印它们。这将产生您期望的输出。
英文:
Pair Class could do the trick here.
import java.util.*;
public class Main
{
static class Pair implements Comparable<Pair>
{
int a1;
int a2;
Pair (int a1, int a2) //constructor
{
this.a1 = a1;
this.a2 = a2;
}
public int compareTo(Pair other) //making it only compare a2 values
{
return this.a2 - other.a2;
}
}
public static void main(String[] args)
{
int[] A1 = {1,2,3,4,5,6,7,8,9,10};
int[] A2 = {1,2,3,0,2,1,1,0,0,0};
Pair[] pairs = new Pair[A1.length];
for (int i = 0; i < pairs.length; i++)
{
pairs[i] = new Pair(A1[i], A2[i]);
}
Arrays.sort(pairs);
//printing values
for (int i = 0; i < A1.length; i++)
{
System.out.print(pairs[i].a1 + " ");
}
System.out.println();
for (int i = 0; i < A2.length; i++)
{
System.out.print(pairs[i].a2 + " ");
}
}
}
By making a Pair class that holds 2 variables a1
and a2
, you can override the compareTo
method to only compare the a2
value, so that when Arrays.sort
is called, the pairs in the Pair array will be swapped only according to the a2
values. You can then access the values in the pairs and print them out. This will produce your desired output.
答案2
得分: 1
你可以创建一个二维数组,其中每个元素都是长度为2
的数组,并根据第二个元素进行排序。
int[] A1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, A2 = { 1, 2, 3, 0, 2, 1, 1, 0, 0, 0 };
final int[][] res = new int[A1.length][2];
for(int i = 0; i < res.length; i++) {
res[i] = new int[] {A1[i], A2[i]};
}
Arrays.sort(res, (a,b)->Integer.compare(a[1], b[1]));
//或者使用 Arrays.sort(res, Comparator.comparingInt(a -> a[1]));
for(final int[] a : res) {
System.out.println(a[0] + " " + a[1]);
}
英文:
You can create a two-dimensional array where each element is an array of length 2
and sort it based on the second element.
int[] A1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, A2 = { 1, 2, 3, 0, 2, 1, 1, 0, 0, 0 };
final int[][] res = new int[A1.length][2];
for(int i = 0; i < res.length; i++) {
res[i] = new int[] {A1[i], A2[i]};
}
Arrays.sort(res, (a,b)->Integer.compare(a[1], b[1]));
//Alternatively, Arrays.sort(res, Comparator.comparingInt(a -> a[1]));
for(final int[] a : res) {
System.out.println(a[0] + " " + a[1]);
}
答案3
得分: 1
你可以尝试根据实际数据创建一个对象。在这种情况下,该对象可能包含两个字段,即“numbers”和“occurrences”。然后实现一个比较器,按照“occurrences”字段进行比较。
英文:
You could try creating an object based on the data that you actually have. In this case the object might contain two fields, numbers and occurrences. Then implement a comparator to compare by the occurrences field.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论