gpt4 book ai didi

java - 用数组填充 HashMap

转载 作者:行者123 更新时间:2023-12-02 05:33:26 24 4
gpt4 key购买 nike

因此,我尝试寻找有关如何执行此操作的教程,但没有什么会变得如此复杂。这是我第一次学习 HashMaps,所以我确信我的解决方案应该很简单,但我不知道该怎么做。

我正在尝试使用数组来填充 HashMap,当我运行该程序时,我的打印输出显示为 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;
}

}

}
}
}

最佳答案

请不要使用raw types 。而且,如果我理解你,那么你想要类似的东西

Map<String, List<Expenses>> map = new HashMap<>();

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

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

关于java - 用数组填充 HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25287445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com