当前位置: 首页 > 知识库问答 >
问题:

但是,将ArrayList转换为HashMap,在ArrayList中选择具有不同变量类的choice对象

白祺然
2023-03-14

我的问题很简单。如何将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;
    }
}

共有1个答案

米修平
2023-03-14

您可以尝试使用一个简单的for each循环:

// 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);
}

helper函数,从这里复制:

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;
}

但要注意,这种方法既不是最佳的,也不是优雅的。我更喜欢使用streamAPI,但这可能并不适合您当前的熟练程度。

 类似资料:
  • 问题内容: 我有arrayList 我想像这样转换为ArrayList到HashMap 请帮助我如何转换为HashMap。 问题答案: 一般的方法是遍历,然后将值插入。示例如下:

  • 我有档案卡。java和Util。java,我不允许修改它。出于我自己的目的,我创建了一个类Card2,它扩展了Card,并添加了一个方法和一个方法。Util类包含一个采用ArrayList的方法 我的理解是,在这种情况下,

  • 我已将此JSON转换为Hashmap http://www.mocky.io/v2/5d0dc72d3400008c00ca4a62 我有嵌套的Hashmap,我想把它转换成ArrayList 我想从获取所有数据并添加到列表中。我还希望hashmap的密钥也将作为指南导入列表中。 这是银行舱 这是银行名单课程 我试过的 但我不明白为什么会有例外 如果可以的话,请给我推荐其他算法

  • 问题内容: Java中是否有命令将ArrayList转换为对象数组。我知道如何将每个对象从arrayList复制到对象数组,但是我想知道是否会自动完成。 我想要这样的东西: 问题答案: 像标准Collection.toArray(T [])之 类的东西应该可以满足您的需求(请注意实现): 附带说明一下,您应该考虑将定义定义为类型,而不是,这样可以避免某些特定于实现的定义,而这些定义可能并不真正适用

  • 问题内容: 将数组元素复制到其中的最佳方法是什么? 我需要快速执行此操作,所以最快的方法是什么? 问题答案: 利用创建 “(通过写‘到数组中。更改到返回的列表)’由指定数组支持的固定大小的列表。” 或者,解耦两个数据结构: 或更简洁:

  • 问题内容: 我是Java的初学者。我真的需要从数组或返回()。有可能的?我无法将数组插入(构造函数)。 代码如下: 问题答案: 添加以下代码以将arraylist值添加至应该可以工作: