java - 向 ArrayList 添加元素失败

标签 java arraylist

我正在研究这个 leetcode problem .

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// Employee info
class Employee {
    // It's the unique id of each node;
    // unique id of this employee
    public int id;
    // the importance value of this employee
    public int importance;
    // the id of direct subordinates
    public List<Integer> subordinates;
};

class Solution {
    public int getImportance(List<Employee> employees, int id) {
        Map<Integer, Integer> values = new HashMap<>();
        Map<Integer, List<Integer>> subordinates = new HashMap<>();

        // put value and subordinates into maps
        for(Employee employee : employees) {
            int e_id = employee.id;
            int value = employee.importance;
            List<Integer> subordinate = employee.subordinates;
            values.put(id, value);
            subordinates.put(id, subordinate);
        }

        // find suborinates and add up the values
        List<Integer> subs = subordinates.getOrDefault(id, new ArrayList<Integer>());
        int total_value = values.getOrDefault(id, 0);

        for(int sub_id : subs){
            total_value = total_value + values.getOrDefault(sub_id, 0);
        }

        return total_value;
    }
}
public class Sixninezero {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Employee e1 = new Employee();
        e1.id = 3;
        e1.importance = 3;
        List<Integer> list_e1 = new ArrayList<>();
        e1.subordinates = list_e1;

        Employee e2 = new Employee();
        e2.id = 2;
        e2.importance = 5;
        List<Integer> list_e2 = new ArrayList<>();
        list_e2.add(3);
        e2.subordinates = list_e2;

        List<Employee> employees = new ArrayList<>();
        employees.add(e1);
        employees.add(e2);

        Solution solution = new Solution();
        int result = solution.getImportance(employees, 2);
        System.out.println(result);
    }

}

我创建了两个 Employee 对象并将它们添加到名为 employees 的 ArrayList 中,然后我通过了employeesgetImportance()方法。在此方法中,for 循环将遍历 employees并输入 id以及相应的value(importance)配对进入名为 values 的 map ,并执行类似于 subordinates 的操作 map 。所以每个valuessubordinates应该有 2 个元素,因为 employees有两个对象。但它总是有一个元素(第二个)。我认为 for 循环 ArrayList 时我做对了,代码有什么问题吗?

最佳答案

每次循环时,您都将方法参数中的 id 放入 values 映射中。 看起来你应该这样做:

values.put( e_id, value );
subordinates.put( e_id, subordinate ); 

这应该可以解决问题!

关于java - 向 ArrayList 添加元素失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57932174/

相关文章:

java - ArrayList<Integer> 的内存使用情况

java - JBoss AS 7.1.1 没有获取我的 JSF 实现

java - 使用 apache poi 从 excel 读取日期格式,无论系统日期格式如何

android - 如何在 AlertDialog 中显示数组列表

java - ArrayList 的容量

java - 找到非托管 pom.xml 文件(无法在 IntelliJ 中导入 Maven 项目)

java - 如何重复直到用户输入一个整数?

java - 空列表的加权顺序

java - 从 R 传递 java arraylist 对象

java - 打印数组列表元素问题