简体   繁体   English

在没有排序的情况下找到重量最高的动物()(地图<string, animal> (Where Animal - 类))</string,>

[英]Find animals with the highest weight without sorted() (map<String, Animal> (Where Animal - class))

I have problem with task.我的任务有问题。 I must find animal with the highest weight but without sorted() in map<String,Animal>.我必须在 map<String,Animal> 中找到重量最高但没有 sorted() 的动物。 Probles is I cannot get the value body_wt.问题是我无法获得值 body_wt。 I tried to make it through double maxWeight = (Collections.max(map.));我试图通过double maxWeight = (Collections.max(map.)); where map Map<String, Animal> but nothing works其中 map Map<String, Animal>但没有任何作用

Class Animal Class 动物

    package com.company;

public class Animal {
    public double body_wt;
    public double brain_wt;
    public double non_dreaming;
    public double dreaming;
    public double total_sleep;
    public double life_span;
    public double gestation;
    public int predation;
    public int exposure;
    public int danger;

    //getters and setters
    @Override
    public String toString()
    {

        return "AnimalCharacteristics{bodyWt="+getValue(body_wt)+", brainWt="+getValue(brain_wt)+", nonDreaming=" +
                getValue(non_dreaming)+ ", dreaming="+ getValue(dreaming)+ ", totalSleep="+getValue(total_sleep)+
                ", lifeSpan="+getValue(life_span)+", gestation="+ getValue(gestation)+", predation="+predation+", exposure="+exposure+", danger="+danger+"}";
    }

}

I read Animals from txt file我从 txt 文件中读取动物

public static Map<String, Animal> readAnimals(){
    Map<String, Animal> animals = new HashMap <>();
    Scanner devScanner = null;
    try {
        devScanner = new Scanner(new File("mammals.txt"));

        String species = "q";
        devScanner.nextLine();
        while (devScanner.hasNext()) {
            Animal animal = new Animal();
            String nextLine = devScanner.nextLine();
            String[] anData = nextLine.split(";");
            for (int i = 0; i < anData.length; i++) {
                if (anData[i].isEmpty()) continue;
                switch(i){
                    case 0:  species = anData[0];continue;
                    case 1: if(anData[1].equals("NA")){animal.setBody_wt(0);} else{ animal.setBody_wt(Double.parseDouble(anData[1]));} continue;
                    case 2: if(anData[2].equals("NA")){animal.setBrain_wt(0);} else{ animal.setBrain_wt(Double.parseDouble(anData[2]));} continue;
                    case 3: if(anData[3].equals("NA")){animal.setNon_dreaming(0);} else{ animal.setNon_dreaming(Double.parseDouble(anData[3]));} continue;
                    case 4: if(anData[4].equals("NA")){animal.setDreaming(0);} else{ animal.setDreaming(Double.parseDouble(anData[4]));} continue;
                    case 5: if(anData[5].equals("NA")){animal.setTotal_sleep(0);} else{ animal.setTotal_sleep(Double.parseDouble(anData[5]));} continue;
                    case 6: if(anData[6].equals("NA")){animal.setLife_span(0);} else{ animal.setLife_span(Double.parseDouble(anData[6]));} continue;
                    case 7: if(anData[7].equals("NA")){animal.setGestation(0);} else{ animal.setGestation(Double.parseDouble(anData[7]));} continue;
                    case 8: if(anData[8].equals("NA")){animal.setPredation(0);} else { animal.setPredation(Integer.parseInt(anData[8]));} continue;
                    case 9: if(anData[9].equals("NA")){animal.setExposure(0);} else { animal.setExposure(Integer.parseInt(anData[9]));} continue;
                    case 10: if(anData[10].equals("NA")){animal.setDanger(0);} else { animal.setDanger(Integer.parseInt(anData[10]));} break;
                }
            }
            animals.put(species, animal);
        }



    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return animals;
}

If I'm understanding correctly, you can make a static method that takes in an animal[].如果我理解正确,您可以制作一个采用动物 [] 的 static 方法。 Have a currentMax value, initialized to Double.MIN_VALUE and for each index in the array, if arr[i].getWeight() > currentMax -> currentMax = arr[i].getWeight().如果 arr[i].getWeight() > currentMax -> currentMax = arr[i].getWeight(),则有一个 currentMax 值,初始化为 Double.MIN_VALUE 和数组中的每个索引。 Then return the max currentMax at the end.然后在最后返回最大 currentMax。 Add an index value if you want the location of it that stores the index of i whenever the currentMax gets updated.如果您想要在 currentMax 更新时存储 i 的索引的位置,请添加一个索引值。

There are many different animals that can have extremely high weights, depending on the species and whether they are domesticated or wild.有许多不同的动物可能具有极高的重量,这取决于物种以及它们是驯养的还是野生的。 Some examples of animals that can weigh a lot include:体重可能很大的动物的一些例子包括:

African elephants can weigh up to around 6,000 kg (13,227 lbs) Blue whales can weigh up to around 200,000 kg (440,925 lbs) Hippopotamuses can weigh up to around 1,500 kg (3,307 lbs) Giraffes can weigh up to around 1,200 kg (2,646 lbs) White rhinoceroses can weigh up to around 2,300 kg (5,071 lbs) Saltwater crocodiles can weigh up to around 1,000 kg (2,205 lbs) Keep in mind that this is not an exhaustive list and there may be other animals that weigh even more.非洲象重达 6,000 公斤(13,227 磅) 蓝鲸重达 200,000 公斤(440,925 磅) 河马重达 1,500 公斤(3,307 磅) 长颈鹿重达 1,200 公斤(2,646 磅)白犀牛的体重可达 2,300 公斤(5,071 磅)左右 咸水鳄的体重可达 1,000 公斤(2,205 磅)左右 请记住,这并不是一个详尽的清单,可能还有其他动物的重量更重。 strong text强文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:在类中找不到 main(String[]) 方法:Animal - error: can't find main(String[]) method in class: Animal 搜索动物方法以获取动物类别 - Search animal method for animal class 在Java动物猜谜游戏中找不到或加载主类 - Could not find or load main class in java animal guessing game AutoValue 示例:错误:找不到符号 class AutoValue_Animal - AutoValue sample: error: cannot find symbol class AutoValue_Animal 公众有什么区别 <T extends Animal> void addAll(List <T> 动物)和public void addAll(List <Animal> 动物)? - What is the difference between public <T extends Animal> void addAll(List<T> animals) and public void addAll(List<Animal> animals)? 为什么我不能创建一个类? 我正在尝试使用 Animal 数组来容纳不同的动物 - Any reason why I can't create a class? I'm trying to have a Animal array to hold different animals 通过类调用动物类的方法A - Calling methodA of Animal class by casting 类型不匹配:无法从地图转换 <String,List<Animal> &gt;到地图 <String,List<Dog> &gt; - Type mismatch: cannot convert from Map<String,List<Animal>> to Map<String,List<Dog>> 泛型:列表<? extends Animal>与列表相同<Animal> ? - Generics : List<? extends Animal> is same as List<Animal>? 是否可以在没有演员的情况下从 Animal 列表中获取 Dog 列表? - Is it possible to get a list of Dog from a list of Animal without a cast?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM