Java 用数组填充哈希映射

Java 用数组填充哈希映射,java,arrays,hashmap,Java,Arrays,Hashmap,所以我试着找到一个关于如何做这件事的教程,但没有什么能变得这么复杂。这是我第一次学习HAshMaps,所以我确信我的解决方案应该很简单,但我不知道如何做到这一点 我试图使用数组填充HashMap,当我运行程序时,我的打印输出显示为null,这表明它不是为我填充的。我已经为此工作了两天,我真的很迷茫 我试图用“类型”来评估我的关键“费用” 编辑:我想把我的案例二打印出来 1:杂货 2:娱乐 3:等等 public static void main(String[] args) throws Fil

所以我试着找到一个关于如何做这件事的教程,但没有什么能变得这么复杂。这是我第一次学习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;
    }

}

}
}
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException、IOException
{
//此处的TODO代码应用程序逻辑
//HashMap=newHashMap();
HashMap=newHashMap();
List expenseType=新建ArrayList();
双倍金额,合计金额;
int cmd,年、月、日;
字符串类型,resp;
总数=0;
String fname=JOptionPane.showInputDialog(“输入预算文件的名称,如果没有文件则无”);
如果(fname.compareTo(“无”)!=0)
{
FileInputStream ist=新的FileInputStream(fname);
ObjectInputStream ifile=新ObjectInputStream(ist);
}
布尔完成=假;
而(!完成)
{
resp=JOptionPane.showInputDialog(“从以下位置输入命令:\n”
+“\t1:添加新的扣减\n”//认为已完成
+“\t2:添加新费用\n”//这已完成,但可以通过
+“\t3:添加存款\n”//此操作已完成
+“\t4:扣减选项\n”
+“\t5:费用选项\n”
+“\t6:银行中的总余额\n”
+“\t7:退出”);
cmd=Integer.parseInt(resp);
开关(cmd)
{
案例1:
打破
案例2:
//提供添加新支出发生的选项。
//提供从支出类型数组中选择的选项。
resp=JOptionPane.showInputDialog(“从以下位置输入命令:\n”
+“\t1:创建新费用\n”//done
+“\t2:从费用列表中选择\n”
+“\t3:退出”);
int cmd2=Integer.parseInt(resp);
交换机(cmd2)
{
案例1:
type=JOptionPane.showInputDialog(“输入费用类型:”);
resp=JOptionPane.showInputDialog(“输入费用金额:”);
金额=Double.parseDouble(resp);
resp=JOptionPane.showInputDialog(“输入支出年份:”);
年份=整数.parseInt(resp);
resp=JOptionPane.showInputDialog(“输入支出月份:”);
月=整数.parseInt(resp);
resp=JOptionPane.showInputDialog(“输入费用日期:”);
日期=整数.parseInt(resp);
//列表费用=map.get(类型);
//地图上有类型列表吗?
如果(expenseType==null){
//不,加一个。
expenseType=新的ArrayList();
map.put(类型、费用类型);
}
费用e=新费用(类型、金额、年、月、日期);
费用类型。添加(e);
//map.put(类型、新阵列列表(费用));
map.put(类型、费用类型);
打破
案例2:
//使用hashmap搜索ArrayLIst和打印输出选项。
//如何填充HashMap?
type=null;
List typelist=map.get(type);//从映射中读取
System.out.println(类型列表);
打破
}
}
}
}
}
请不要使用。如果我理解你的意思,那么你想要

Map<String, List<Expenses>> map = new HashMap<>();
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) 你应该有这条线

map.put(new String(type),expenses);
而不是

map.put(expenses, new String(type));
使用键(即类型)从hashmap获取值

2) 同时从中删除双引号

List<Expenses> typelist = map.get("type");
List-typelist=map.get(“type”);

传递变量类型。

如果您使用
费用
对象作为键,这意味着当您查找某个对象时,您将
费用
对象提供给映射,它将返回“类型”。这是您想要做的吗?你说的数组在哪里?好的,在做了以下调整后,我仍然得到一个“null”输出。只是澄清一下,“map”被认为是原始类型?@JPHochbaum否,这个
List expenses=new ArrayList()
是一个原始的
数组列表
@JPHochbaum
映射不是
map.get(类型)@JPHochbaum很好。现在删除
type=null
映射获取(类型)您可以在从映射中获取值后将其设为null。但是如果我删除type=null,则map.get(type)将变得未初始化。这也会给我“type”未初始化的错误。我不认为这是必要的,因为我已经在案例1中设置了类型。对这个错误感到困惑。@JPHochbaum提出了不同的问题,但编译器不知道案例1将始终执行。将
type
声明为,
String type=null和该消息将消失。