英文:
How to sort one array based on another, when elements repeat, in Java?
问题
以下是翻译好的部分:
我有两个ArrayList
A:5 3 2 6 1 4
B:0 1 2 0 3 2
我想根据A中的相应值对B进行排序,所以我应该得到:3 2 1 2 0 0
当我使用以下代码时:
ArrayList<Integer> D=new ArrayList<Integer>(B);
Collections.sort(B, Comparator.comparing(s -> A.get(D.indexOf(s))));
或者:
ArrayList<Integer> D = new ArrayList<Integer>(B);
Collections.sort(B, new Comparator<Integer>(){
public int compare(Integer a,Integer b){
return Integer.compare(A.get(D.indexOf(a)),A.get(D.indexOf(b)));
}
});
如果B中的元素是唯一的,那么这将起作用,但由于2和0都出现了2次,每次调用A.get(D.indexOf(2))
时,都会返回2,而4永远不会返回。
所以我最终得到:3 2 2 1 0 0
有人可以帮我解决这个问题吗?我不想编写完整的排序算法,但这样的解决方案也是可以的。
英文:
I have two ArrayLists
A: 5 3 2 6 1 4
B: 0 1 2 0 3 2
I want to sort B, based on corresponding values from A, so I should get : 3 2 1 2 0 0
When I use the following codes :
ArrayList<Integer> D=new ArrayList<Integer>(B);
Collections.sort(B, Comparator.comparing(s -> A.get(D.indexOf(s))));
or :
ArrayList<Integer> D = new ArrayList<Integer>(B);
Collections.sort(B, new Comparator<Integer>(){
public int compare(Integer a,Integer b){
return Integer.compare(A.get(D.indexOf(a)),A.get(D.indexOf(b)));
}
});
It would have worked if the elements in B were unique, but since both 2 and 0 has 2 occurances, every time A.get(D.indexOf(2))
is called, 2 is returned and 4 is never returned.
So I finally get : 3 2 2 1 0 0
Can anyone help me with a comparator that deals with this? I don't want to make a full sorting algorithm, but such solutions are also welcome.
答案1
得分: 2
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list1 = List.of(5, 3, 2, 6, 1, 4);
List<Integer> list2 = List.of(0, 1, 2, 0, 3, 2);
List<Integer> tempList = new ArrayList<>();
for (Integer i : list1) {
tempList.add(list2.get(i - 1));
}
System.out.println(tempList);
}
}
**Output:**
[3, 2, 1, 2, 0, 0]
**[Update]**
Given below is an updated solution to meet OP's new requirement mentioned in the comment below the answer.
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args){
List<Integer> list1 = List.of(15, 13, 12, 16, 11, 14);
List<Integer> list2 = List.of(0, 1, 2, 0, 3, 2);
List<Integer> tempList = new ArrayList<>();
int max = Collections.max(list1);
int offset = max - list2.size() + 1;
for (Integer i : list1) {
tempList.add(list2.get(i - offset));
}
System.out.println(tempList);
}
}
**Output:**
[3, 2, 1, 2, 0, 0]
英文:
A simple way to do it as follows:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> list1 = List.of(5, 3, 2, 6, 1, 4);
List<Integer> list2 = List.of(0, 1, 2, 0, 3, 2);
List<Integer> tempList = new ArrayList<>();
for (Integer i : list1) {
tempList.add(list2.get(i - 1));
}
System.out.println(tempList);
}
}
Output:
[3, 2, 1, 2, 0, 0]
[Update]
Given below is an updated solution to meet OP's new requirement mentioned in the comment below the answer.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args){
List<Integer> list1 = List.of(15, 13, 12, 16, 11, 14);
List<Integer> list2 = List.of(0, 1, 2, 0, 3, 2);
List<Integer> tempList = new ArrayList<>();
int max = Collections.max(list1);
int offset = max - list2.size() + 1;
for (Integer i : list1) {
tempList.add(list2.get(i - offset));
}
System.out.println(tempList);
}
}
Output:
[3, 2, 1, 2, 0, 0]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论