我的任务有问题。我必须在map〈String,Animal〉中找到权重最大但没有sorted()的动物。问题是我无法获得body_wt值。我尝试通过**double maxWeight =(Collections.max(map.))来获得它;where mapMap〈字符串,动物〉**但没有任何效果
动物分类
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+"}";
}
}
我从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;
}
2条答案
按热度按时间sdnqo3pr1#
如果我没理解错的话,你可以创建一个静态方法,它接收一个animal[].有一个currentMax值,初始化为Double.MIN_VALUE,对于数组中的每个索引,如果arr[i].getWeight()〉当前最大值-〉当前最大值= arr[i].getWeight()。然后在末尾返回最大值currentMax。如果您想要在currentMax更新时存储i的索引的位置,请添加索引值。
wwwo4jvm2#
有许多不同的动物可以有极高的体重,这取决于它们的种类以及它们是驯养的还是野生的。一些可以有很大体重的动物的例子包括:
非洲象的体重可达6,000公斤左右(13,227磅)蓝鲸体重可达20万公斤左右河马的体重可达1,500公斤左右长颈鹿的体重可达1,200公斤左右(2,646磅)白色犀牛体重可达2,300公斤左右(5,071磅)咸水鳄的体重可达1,000公斤左右(2,205磅)请记住,这不是一个详尽的列表,可能还有其他动物的体重甚至更重。strong text