【问题标题】:Java HashMap Create, edit and deleteJava HashMap 创建、编辑和删除
【发布时间】:2012-02-12 21:30:53
【问题描述】:

我正在尝试更新一个HashMap,直接在下一个方法中使用它,但它不起作用。根据我阅读的内容,我找不到解决方案。有人说这是不可能的,有人说使用迭代器,但即使使用迭代器也无法正常工作。错误是打印方法它没有打印,甚至没有进入while循环,因为它是空的,但我找不到原因

这是我尝试更新和打印一些信息的两种方法。

  import java.io.File;
     import java.util.ArrayList;
     import java.util.HashMap;
     import java.util.Iterator;
     import java.util.Scanner;
     import java.util.Enumeration;
     import java.util.Hashtable;
     import java.util.Iterator;
     import java.util.Map;
     import java.util.Set;

    public class OrderList {
    // Storage for an arbitrary number of details.

    private HashMap<String, Order> orderList = new HashMap<String, Order>();

    /**
     * Perform any initialization .
     */
    public OrderList() {
        orderList = new HashMap<String, Order>();
    }

    public HashMap<String, Order> getOrders() {
        return orderList;

    }

    public void readOrderFile(String OrderListPath) {
        try {
            File file = new File(OrderListPath);
            Scanner scan = new Scanner(file);
            while (scan.hasNextLine()) {
                String readLine = scan.nextLine();

                if (readLine != null) {

                    getSplitLinesOrders(readLine);
                }
            }

        } catch (Exception e) {
        }
    }

    public void getSplitLinesOrders(String readLine) {
        String id = "";
        String customerId = "";
        String itemId = "";
        int quantity = 0;
        try {
            String[] splitedLine = readLine.split(",");

            if (splitedLine.length == 4) {
                id = splitedLine[0];
                customerId = splitedLine[1];
                itemId = splitedLine[2];
                quantity = Integer.parseInt(splitedLine[3]);
                Order newOrder = new Order(id, customerId, itemId, quantity);
                orderList.put(id, newOrder);
            }
        } catch (Exception e) {
        }


    }

    /**
     * Add a new set of details to the list
     * @param details The details of the staff
     */
//    public void addDetails(Order details) {
//        orderList.add(details);
//    }
    public boolean hasOrder() {
        return orderList.size() != 0;
    }

    public Order getNextOrder() {
        Order order = orderList.remove(0);
        return order;

    }

    /**
     * @return All the details
     */
    public String listDetails() {
        StringBuffer allEntries = new StringBuffer();
        for (Map.Entry<String, Order> details : orderList.entrySet()) {
            String Key = details.getKey();
            Object value = details.getValue();
            allEntries.append(Key + " " + value);
        }
        return allEntries.toString();
    }

    public void PrintListOfOrders() {
        Iterator it = getOrders().entrySet().iterator();
        try {

            while (it.hasNext()) {
                Order value = (Order) it.next();
                System.out.println(value.getOrderId() + " " + value.getCustomerId() + " " +     value.getItemId() + " " + value.getQuantity());
            }




        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

【问题讨论】:

  • 您的代码根本不包含地图,这使得帮助您有些棘手。请阅读tinyurl.com/so-hints
  • 对不起,我忘了放地图的代码
  • 你也忘了告诉我们是什么不工作......

标签: java iterator hashmap


【解决方案1】:

您可能收到NullPointerException?下次告诉我们出了什么问题并提供堆栈跟踪(如果适用)。

您发布的代码不会创建orderList 的实例,因此如果未在其他地方完成,该代码将抛出NullPointerException

尝试添加:

 private HashMap<String, Order> orderList = new HashMap<String, Order>;

像这样吞下Exception

} catch (Exception e) {
}

这不是一个好的做法,因为它会隐藏所有关于问题所在的信息,至少这样做:

catch (Exception e) {
   e.printStacktrace();

}

【讨论】:

  • 好的,我更改了代码,但它仍然无法正常工作或显示任何错误
  • 仍然很难为您提供更多帮助,因为您没有指定 WHAT 不起作用。例如,您如何调用您的方法?
  • 我尝试了以下方法: OrderList l = new OrderList(); l.getSplitLinesOrders("彼得,213,123,123"); System.out.println(l.listDetails());它产生了“Order@71060478”(我不得不创建一个 Order 类,因为你的代码中缺少它)。什么情况下不工作
【解决方案2】:

你可以这样做:

Set<String> s = List.keySet();
Iterator<String> i = s.iterator();

这是迭代器的初始化,然后你可以使用i.next()遍历key,每次获取一个String,并要求orderList.get(thatString)获取值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2023-03-31
    • 2020-10-15
    相关资源
    最近更新 更多