This is a project I am working on for my intro to java class. My professor has already laid out the base code, and the point of the project is to work with HashMaps and ArrayLists in combination with arithmetic. Everything here is done by my professor so far except:
HashMap<String, Integer> typeAttack = new HashMap<String, Integer>();
I am also provided with a .csv file containing various statistics of a whole list of pokemon. Out of the objects that my professor has already passed into the ArrayList "pokemonList," I only need to consider the "type" and "attack" variable, as I need to figure out which type of pokemon in the whole .csv file averages to have the highest attack level.
int attack = Integer.parseInt(split[1]);
String type = split[5];
My question is very simple. How can I convert only a portion of the ArrayList, specifically the "attack" and "type" variables into my 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;
}
}
POKEMON CLASS
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;
}
}
You could try with a simple 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);
}
The helper function, copied from 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;
}
Be aware though that this approach is neither optimal nor elegant. I'd prefer to use Stream
API, but that may not be best suited for your current proficiency.
EDIT: I've adjusted the code not to use Java 8 methods.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加