如何打印出一次数组元素的相同名称

时间:2014-08-11 19:59:01

标签: java arrays

我正在制作预算应用以进行练习。我正在使用一个对象数组,希望能够迭代一个数组,只打印出一个同名的元素,创建一个供用户选择的菜单,而无需对其进行硬编码。

我希望我的输出看起来像这样:

  1. 杂货
  2. 餐馆
  3. 其他费用。
  4. 未列出。
  5. 所以我想避免多次列出“费用类型”。合理?这是我到目前为止的代码:

    package homebudget;
    
    import java.io.FileInputStream;
    import java.io.ObjectInputStream;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import javax.swing.JOptionPane;
    
    /**
    *
    * @author Derek
    */
    public class HomeBudget 
    {
    //Features to add:
    //Reminder 2 days before an auto deduction
    
    
    
    
    public static void main(String[] args) throws Exception
    {
        // TODO code application logic here
    
    List<Bills> deductions = new ArrayList();
    String billName, deductDate, resp;
    double amount, totalAmount;
    int cmd, year, month, date;
    totalAmount = 0;
    
    List<Spending> expenses = new ArrayList();
    String type;
    
    List<Income> deposits = new ArrayList();
    String incomeType;
    
    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);
            deductions = (ArrayList<Bills>) ifile.readObject();
    
        }
        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:
    
                billName = JOptionPane.showInputDialog("Enter the name of the bill:");
                deductDate = JOptionPane.showInputDialog("Enter the deduct date:");
                resp = JOptionPane.showInputDialog("Enter the deduct amount");
                amount = Double.parseDouble(resp);
    
                Bills d = new Bills(billName, deductDate, amount);
                deductions.add(d);
                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);
                        Spending s = new Spending(amount, type, year, month, date);
                        expenses.add(s);    
    
    
                    case 2:
    
    
                    Iterator<Spending> spendIter = expenses.iterator();
    
                    boolean found = false;
                    while(!found && spendIter.hasNext())
                    {
                      s = spendIter.next();
                      if(s.getType().compareTo(type) == 0)
                      {
                        JOptionPane.showInputDialog("Choose from list of expenses below:" + s.getType());
    
                        found = true;
                      }
                    }
                      if(!found)
                      {
                          System.out.println("No expenses exist.");
                      }
                        //This is the part that I have no idea how to code.
                        //I want a list of expenses to be shown where the user can select a     number and that 
                        //expense is selected.   
    
    
                break;
    

2 个答案:

答案 0 :(得分:0)

将数组的元素放入哈希表中,然后遍历其元素。哈希表确保与重复键对应的值放在同一个地方,从而实质上隐含地删除重复项。以下是对它的简单演示。

int[] a = {1, 1, 2, 1, 2, 3, 1, 2, 3, 4};
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < a.length; i++) {
    map.put(a, a);
}
for (Integer x : map.keySet()) {
    System.out.println(map.get(x));
}

答案 1 :(得分:0)

您可以维护费用类型的set,这样您就可以确保只使用新类型,而不是已包含的类型。