Java 但是,要将ArrayList转换为HashMap,请在ArrayList中选择具有不同变量类的choice对象
这是一个项目,我正在为我的java简介类工作。 我的教授已经列出了基本代码,该项目的重点是将HashMaps和ArrayList与算术相结合。 到目前为止,这里的一切都是由我的教授完成的,除了:Java 但是,要将ArrayList转换为HashMap,请在ArrayList中选择具有不同变量类的choice对象,java,grouping,Java,Grouping,这是一个项目,我正在为我的java简介类工作。 我的教授已经列出了基本代码,该项目的重点是将HashMaps和ArrayList与算术相结合。 到目前为止,这里的一切都是由我的教授完成的,除了: HashMap typeAttack=newhashmap() 我还提供了一个.csv文件,其中包含了口袋妖怪整个列表的各种统计信息。 在我的教授已经进入ARAYLIST“PokEMONLIST”的对象中,我只需要考虑“类型”和“攻击”变量,因为我需要找出整个类型的Pokimon。CSV文件平均具有最高
HashMap typeAttack=newhashmap()代码>
我还提供了一个.csv文件,其中包含了口袋妖怪整个列表的各种统计信息。
在我的教授已经进入ARAYLIST“PokEMONLIST”的对象中,我只需要考虑“类型”和“攻击”变量,因为我需要找出整个类型的Pokimon。CSV文件平均具有最高的攻击级别。
int-attack=Integer.parseInt(拆分[1])代码>
字符串类型=拆分[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;
}
}
您可以尝试一个简单的方法:
//注意映射类型更改为Double!
HashMap typeToAvgAttack=新HashMap();
//用于存储每种类型的所有攻击值列表的中间映射
HashMap typeToAttack=新HashMap();
用于(口袋妖怪口袋妖怪:口袋妖怪列表){
字符串类型=pokemon.getType();
int-attack=pokemon.getAttack();
//地图一开始是空的,我们需要检查钥匙(口袋妖怪类型)是否存在
List attackValues=typeToAttack.get(type);
if(attackValues==null){
put(type,attackValues=newarraylist());
}
添加(攻击);
}
//迭代贴图键以计算平均值
for(字符串类型:typeToAttack.keySet()){
List attackValues=typeToAttack.get(type);
双平均值=计算平均值(攻击值);
类型toavgattack.put(类型,平均值);
}
助手函数,复制自:
公共静态双重计算范围(列表值){
双和=0d;
如果(!values.isEmpty()){
for(整数值:值){
总和+=数值;
}
返回sum/values.size();
}
回报金额;
}
请注意,这种方法既不是最佳的,也不是优雅的。我更喜欢使用Stream
API,但这可能不适合您目前的熟练程度
编辑:我已将代码调整为不使用Java 8方法。您可以循环浏览列表。对于列表中的每一个口袋妖怪,你都会得到它的攻击,你也会得到它的类型(通过调用口袋妖怪类的方法来获取这些信息)。然后你做你需要做的(你没有说过)来填充地图。从更简单的问题开始。1.循环浏览列表,打印每个口袋妖怪。2.循环浏览列表,并打印每个口袋妖怪的攻击类型。你能显示一下口袋妖怪
类看起来像什么吗我不明白,但是。。。他创造的方法是什么?看起来它只是将.csv文件的元素排序到ArrayList中。我怎样才能称之为获得单个元素?而且你不需要地图来确定哪一个口袋妖怪的攻击级别最高。你可以通过一个简单的循环来完成这项工作。我被告知要使用HashMaps和ArrayLists来完成这项工作,而不是使用单个变量进行硬编码。这很有帮助。我目前的知识不允许我超越您的第一个Enced for循环,但前两行有帮助。谢谢。我已经更新了答案,试图更明确一些。如果还有不清楚的地方,请告诉我。没有理由将sum
声明为已装箱的Integer
而不是直接的int
。而不是if(typeToAttack.containsKey(type)){List-attackValues=typeToAttack.get(type);attackValues.add(attack);}否则{List-attackValues=new-ArrayList();attackValues.add(attack);typeToAttack.put(type,attackValues);}
您可以使用List-attackValues=typeToAttack.get(type);if(attackValues==null)typeToAttack.put(type,attackValues=newarraylist());添加(攻击)代码>@Holger第二部分我同意,编辑答案。我从链接的anwser复制的助手函数。所以,也许你应该把评论放在那里,到目前为止,它已经得到了64分。讨论中可能有比这里更合适的人;)。再次粘贴链接以获得链接答案,作者将int
更改为Integer
,以修复调用sum.doubleValue()
。但是您可以使用类型转换来替换方法调用,即改为(double)sum
,然后它可以使用int sum顺利工作
或只使用双和代码>首先。我也在那里留下了评论,但没有理由保留有问题的代码,因为它也在另一个答案中…
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;
}
}
// 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);
}
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;
}