Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何一次打印出数组元素的相同名称_Java_Arrays - Fatal编程技术网

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

Java 如何一次打印出数组元素的相同名称,java,arrays,Java,Arrays,我正在开发一个预算应用程序进行练习。我使用的是一个对象数组,希望能够遍历数组,只打印出一次同名元素,创建一个菜单供用户选择,而无需硬编码 我希望我的输出如下所示: 杂货 餐厅 其他费用。 没有列出。 因此,我希望避免费用类型被多次列出。有道理?以下是我目前掌握的代码: package homebudget; import java.io.FileInputStream; import java.io.ObjectInputStream; import java.util.ArrayList;

我正在开发一个预算应用程序进行练习。我使用的是一个对象数组,希望能够遍历数组,只打印出一次同名元素,创建一个菜单供用户选择,而无需硬编码

我希望我的输出如下所示:

杂货 餐厅 其他费用。 没有列出。 因此,我希望避免费用类型被多次列出。有道理?以下是我目前掌握的代码:

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;

将数组的元素放入哈希表中,然后迭代其元素。哈希表确保与重复键对应的值放在同一位置,因此本质上隐式删除重复项。下面是一个简单的演示

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

您可以维护费用类型的集合,这样可以确保只使用新类型,而不是它已经包含的类型

请参阅使用集合来跟踪您已经看到的元素。我们只在类中简要介绍了哈希表。所以我需要练习使用这些,谢谢你的建议!在HashMap的键和值中放置相同的整数有什么意义?这是不必要的。这是一个简单的例子。。。在这种情况下,您可以将null作为值…好吧,刚才做了一个快速教程,您建议使用hashMap,因为它可以消除重复项?是的。。。任何地图或布景都可以。。。您可以对每个。。。i、 你有4个选择。。。aHashMap iiTreeMap iii HashSet iv TreeSetAnd,那么我也可以为一个集合使用多个值?对于费用,我计划确定与费用相关的金额和日期。我没有完全理解你的问题。集合的众多值也意味着金额和日期的不同集合?基本上,用户将选择一种费用类型,并输入费用的金额和日期。每个费用类型有两个值,金额和日期。好的,我的答案应该是: