Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何根据另一个已排序的数组按字母顺序对数组进行排序?_Java_Arrays_Sorting - Fatal编程技术网

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())

输出 类水果 比较器 如果您想按另一个ciriterion排序,只需在此处调整此代码

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;
              }
        }
    }