Java 如何根据另一个已排序的数组按字母顺序对数组进行排序?
我有两个阵列:Java 如何根据另一个已排序的数组按字母顺序对数组进行排序?,java,arrays,sorting,Java,Arrays,Sorting,我有两个阵列: int res [] = new int [4]; String str [] = new String [4]; res[]的内容: res[0] = 2 res[1] = 2 res[2] =1 res[3] =3 str[]的内容: str[1] = Cherry str[2] = Banana str[3] = Apple str[4] = Grapes 我想将str[]的内容连接到res[],例如: str[0] is to res[0], str[1] is t
int res [] = new int [4];
String str [] = new String [4];
res[]的内容:
res[0] = 2
res[1] = 2
res[2] =1
res[3] =3
str[]的内容:
str[1] = Cherry
str[2] = Banana
str[3] = Apple
str[4] = Grapes
我想将str[]的内容连接到res[],例如:
str[0] is to res[0], str[1] is to res[1] and so on...
我怎样才能打印出这样的东西
Grapes 3
Banana 2
Cherry 2
Apple 1
当res
相对于其str
对应项按降序排列时,如果res
值重复,则按str
的字典顺序打印
或者可以有一种不同的方法,而不是使用两个单独的数组?您可以通过将
res
和str
数组添加到HashMap
中,然后按值对HashMap排序(将HashMap转换为entryset)
请查找代码:
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
public class Test {
public static void main(String[] args) {
int res [] = new int [4];
String str [] = new String [4];
res[0] = 2;
res[1] = 2;
res[2] =1;
res[3] =3;
str[0] = "Cherry";
str[1] = "Banana";
str[2] = "Apple";
str[3] = "Grapes";
HashMap<String,Integer> hashmap = new HashMap<>();
for(int i =0;i<str.length;i++){
hashmap.put(str[i],res[i]);
}
Function<Map.Entry,Map.Entry> function = null;
hashmap.entrySet().stream()
.sorted((k1, k2) -> k1.getValue().equals(k2.getValue())?
k1.getKey().compareTo(k2.getKey()):-k1.getValue().compareTo(k2.getValue()))
.forEach(k -> System.out.println(k.getKey() + ": " + k.getValue()));
}
}
import java.util.HashMap;
导入java.util.Map;
导入java.util.function.function;
公开课考试{
公共静态void main(字符串[]args){
int res[]=新的int[4];
字符串str[]=新字符串[4];
res[0]=2;
res[1]=2;
res[2]=1;
res[3]=3;
str[0]=“Cherry”;
str[1]=“香蕉”;
str[2]=“苹果”;
str[3]=“葡萄”;
HashMap HashMap=新的HashMap();
对于(int i=0;i k1.getValue().equals(k2.getValue())?
k1.getKey().compareTo(k2.getKey()):-k1.getValue().compareTo(k2.getValue())
.forEach(k->System.out.println(k.getKey()+“:”+k.getValue());
}
}
解决方案
要归档所需结果,还可以使用对象数组
- 创建一个具有两个属性的类,第一个属性是值,第二个属性是水果名,还要为这些属性编写getter方法
- 编写一个比较器,根据对象的值对对象进行排序
- 循环遍历对象数组,并使用
System.out.println(frutp.getVal()+“”+frutp.getFruit())代码>
import java.util.Comparator;
public class SortByVal implements Comparator<FruitP> {
@Override
public int compare(FruitP o1, FruitP o2) {
// TODO Auto-generated method stub
if(o2.val > o1.val) {
return 1;
}else if(o2.val < o1.val) {
return -1;
}else {
return 0;
}
}
}
import java.util.Comparator;
公共类SortByVal实现了Comparator{
@凌驾
公共int比较(FROUTPO1,FROUTPO2){
//TODO自动生成的方法存根
如果(o2.val>o1.val){
返回1;
}否则如果(o2.val
不要使用两个不同的数组,而是使用一个包含两个数据的类,然后搜索关于如何按字段对类实例列表进行排序的许多问题使用HashMap、2D对象数组等。为strI添加的字典顺序已更新了代码,您能检查一下吗。对HashMap进行排序这是什么意思?Hashmap没有顺序,它是否回答了您的问题?
3 Grapes
2 Cherry
2 Banana
1 Apple
public class FruitP {
public int val;
public String fruit;
public FruitP(int val, String fruit) {
this.val = val;
this.fruit = fruit;
}
public int getVal() {
return this.val;
}
public String getFruit() {
return this.fruit;
}
}
import java.util.Comparator;
public class SortByVal implements Comparator<FruitP> {
@Override
public int compare(FruitP o1, FruitP o2) {
// TODO Auto-generated method stub
if(o2.val > o1.val) {
return 1;
}else if(o2.val < o1.val) {
return -1;
}else {
return 0;
}
}
}