gpt4 book ai didi

java - 然而,将 ArrayList 转换为 HashMap,在 ArrayList 中选择具有不同变量类的选择对象

转载 作者:行者123 更新时间:2023-11-30 05:24:33 24 4
gpt4 key购买 nike

这是我正在为我的 java 类入门而做的一个项目。我的教授已经布置了基本代码,该项目的重点是将 HashMap 和 ArrayList 与算术结合使用。到目前为止,这里的所有内容都是由我的教授完成的,除了:

HashMap<String, Integer> typeAttack = new HashMap<String, Integer>();

我还获得了一个 .csv 文件,其中包含整个神奇宝贝列表的各种统计数据。在我的教授已经传递到 ArrayList“pokemonList”的对象中,我只需要考虑“类型”和“攻击”变量,因为我需要弄清楚整个 .csv 文件中哪种类型的 pokemon 平均为具有最高的攻击等级。

int attack = Integer.parseInt(split[1]);

String type = split[5];

我的问题很简单。如何仅将 ArrayList 的一部分(特别是“攻击”和“类型”变量)转换为我的 HashMap?

import java.util.*;
import java.io.*;

public class Project6 {

public static void main(String[] args) throws IOException {
ArrayList<Pokemon> pokemonList = collectPokemon(args[0]);


HashMap<String, Integer> typeAttack = new HashMap<String, Integer>();


}

// Don't modify this method. If you get errors here, don't forget to add the filename
// as a command line argument.
public static ArrayList<Pokemon> collectPokemon(String filename) throws IOException {
BufferedReader file = new BufferedReader(new FileReader(new File(filename)));
ArrayList<Pokemon> pokemonList = new ArrayList<Pokemon>();
file.readLine();
while(file.ready()) {
String line = file.readLine();
String[] split = line.split(",");
String name = split[0];
int attack = Integer.parseInt(split[1]);
int defense = Integer.parseInt(split[2]);
double height = Double.parseDouble(split[3]);
double weight = Double.parseDouble(split[6]);
String type = split[5];
Pokemon current = new Pokemon(name, attack, defense, height, weight, type);
pokemonList.add(current);
}
return pokemonList;
}
}

口袋妖怪等级

import java.util.*;

public class Pokemon {

private String name;
private int attack;
private int defense;
private double height;
private double weight;
private String type;

public Pokemon(String inName, int inAttack, int inDefense, double inHeight, double inWeight, String inType) {
name = inName;
attack = inAttack;
defense = inDefense;
height = inHeight;
weight = inWeight;
type = inType;
}

public String getName() {
return name;
}

public int getAttack() {
return attack;
}

public int getDefense() {
return defense;
}

public double getHeight() {
return height;
}

public double getWeight() {
return weight;
}

public String getType() {
return type;
}

public String toString() {
return "Pokemon: '" + name + "' Atk: " + attack + " Def: " + defense + " Ht: " + height + "m Wt: " + weight + "Kg Type: " + type;
}
}

最佳答案

您可以尝试使用简单的 for each loop :

// Note the Map type change to Double!
HashMap<String, Double> typeToAvgAttack = new HashMap<String, Double>();

// Intermediate map to store list of all attack values per type
HashMap<String, List<Integer>> typeToAttack = new HashMap<String, List<Integer>>();

for (Pokemon pokemon: pokemonList) {
String type = pokemon.getType();
int attack = pokemon.getAttack();

// the map is empty at start, we need to check for the keys (pokemon type) existance
List<Integer> attackValues = typeToAttack.get(type);
if (attackValues == null) {
typeToAttack.put(type, attackValues = new ArrayList());
}
attackValues.add(attack);
}

// Iterate over map keys to calculate the average
for (String type : typeToAttack.keySet()) {
List<Integer> attackValues = typeToAttack.get(type);
double average = calculateAverage(attackValues);
typeToAvgAttack.put(type, average);
}

辅助函数,复制自here :

public static double calculateAverage(List <Integer> values) {
double sum = 0d;
if(!values.isEmpty()) {
for (Integer value: values) {
sum += value;
}
return sum / values.size();
}
return sum;
}

但请注意,这种方法既不是最佳的,也不是优雅的。我更愿意使用 Stream API,但这可能不太适合您当前的熟练程度。

编辑:我已调整代码以不使用 Java 8 方法。

关于java - 然而,将 ArrayList 转换为 HashMap,在 ArrayList 中选择具有不同变量类的选择对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58924416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com