使用数组填充HashMap

时间:2014-08-13 13:23:14

标签: java arrays hashmap

所以我试着找一个关于如何做到这一点的教程,但没有一件事变得复杂。这是我第一次学习HAshMaps,所以我相信我的解决方案应该很简单,但我不知道该怎么做。

我正在尝试使用数组来填充HashMap,当我运行程序时,我的print out显示为null,这表示它不会填充给我。已经工作了两天,我真的迷茫和迷茫。

我试图让我的关键“费用”用“类型”来估价。

编辑:我希望我的案例二是打印输出 1:杂货 2:娱乐 3:等等......

public static void main(String[] args) throws FileNotFoundException, IOException 
{
    // TODO code application logic here
//        HashMap<String, String> map = new HashMap<String, String>();
    HashMap<String, List<Expenses>> map = new HashMap<>();
    List <Expenses> expenseType = new ArrayList();
    double amount, totalAmount;
    int cmd, year, month, date;
    String type, resp;
    totalAmount = 0;

    String fname = JOptionPane.showInputDialog("Enter the name of the budget file, none if no file");
    if (fname.compareTo("none") !=0)
    {
        FileInputStream ist = new FileInputStream(fname);
        ObjectInputStream ifile = new ObjectInputStream(ist);


    }
    boolean done = false;
    while(!done)
    {
        resp = JOptionPane.showInputDialog("Enter a command from: \n" 
                + "\t1:Add a new deduction\n"  //think its done
                + "\t2:Add a new expense\n"  //this is done, but could be made better wit
                + "\t3:Add a deposit\n"  //This is done
                + "\t4:Deduction options\n"  
                + "\t5:Expense Options\n"  
                + "\t6:Total balances in bank\n"
                + "\t7:quit");
        cmd = Integer.parseInt(resp);

         switch(cmd)
        {
            case 1:


            break;

            case 2:
            //Give the option to add new spending occurence.
            //Give option to choose from array of spending types.
            resp = JOptionPane.showInputDialog("Enter a command from: \n" 
                + "\t1: Create a new expense\n"  //done
                + "\t2: Choose from expense list\n"
                + "\t3:quit");
            int cmd2 = Integer.parseInt(resp);
            switch (cmd2)
            {
                case 1:

                 type = JOptionPane.showInputDialog("Enter the type of the expense:"); 

                    resp = JOptionPane.showInputDialog("Enter the amount of the expense:");   
                    amount = Double.parseDouble(resp);
                    resp = JOptionPane.showInputDialog("Enter the year of the expense:");
                    year = Integer.parseInt(resp);
                    resp = JOptionPane.showInputDialog("Enter the month of the expense:");
                    month =  Integer.parseInt(resp);
                    resp = JOptionPane.showInputDialog("Enter the date of the expense:");
                    date =  Integer.parseInt(resp);
//                      List<Expenses> expenses = map.get(type);
                    // Does the map have a List for type?
                    if (expenseType == null) {
                     // No. Add one.
                     expenseType = new ArrayList<>();
                        map.put(type, expenseType);
                        }
                    Expenses e = new Expenses(type, amount, year, month, date);
                    expenseType.add(e);

//                        map.put(type, new ArrayList(expenses));
                    map.put(type, expenseType);

                    break;

                case 2:

                 //Use a hashmap to search through the ArrayLIst and print out options.
                    //How do I populate the HashMap?
                 type = null;
                 List<Expenses> typelist = map.get(type); //reads from map
                 System.out.println(typelist);


            break;
    }

}

}
}
}

2 个答案:

答案 0 :(得分:1)

请不要使用raw types。而且,如果我了解你,那么你需要像

这样的东西
Map<String, List<Expenses>> map = new HashMap<>();

然后,要添加到List中的Map,请使用类似

的内容
List<Expenses> expenses = map.get(type);
// Does the map have a List for type?
if (expenses == null) {
  // No. Add one.
  expenses = new ArrayList<>();
  map.put(type, expenses);
}
Expenses e = new Expenses(type, amount, year, month, date);
expenses.add(e);

答案 1 :(得分:1)

1)你应该有这一行

map.put(new String(type),expenses);

而不是

map.put(expenses, new String(type));

使用键即从hashmap获取值,即类型。

2)同时从

中删除双引号
List<Expenses> typelist = map.get("type");

传递变量类型。