【问题标题】:Java: Adding new items in a HashMap [closed]Java:在 HashMap 中添加新项目 [关闭]
【发布时间】:2014-03-07 17:56:06
【问题描述】:

对于学校,我必须编写一个程序,该程序将从文件中获取值,然后将它们打印出来。我使用的是HashMap,因为它最适合存储我的数据。我遇到的问题是,当我尝试添加它时,我得到一个空指针。也许这是因为整数没有被实例化,但我该怎么做呢? 这是我的代码:

public class Driver {

static ArrayList<Event> myEvents = new ArrayList<Event>();
static String[][][] counts = new String[50][1000][2];


//static ArrayList<int>[] myCounts = new ArrayList[];
//static ArrayList<String>[] myCountProfile = new ArrayList[];

@SuppressWarnings("unchecked")
static HashMap<String, Integer>[] count = new HashMap[50];

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    File input = new File("./src/Problem1/stormdata_2013.csv");
    Scanner fileRead = new Scanner(input);


    while (fileRead.hasNextLine()) {
        String currentLine = fileRead.nextLine();
        Scanner lineScan = new Scanner(currentLine).useDelimiter(",");

        // Dealing with unnecessary information
        for (int i = 0; i < 8; i++) {
            lineScan.next();
        }
        String state = lineScan.next().trim();
        int stateNum = Integer.parseInt(lineScan.next());
        lineScan.next();
        String month = lineScan.next().trim();
        String type = lineScan.next().trim();

        myEvents.add(new Event(type, month, state, stateNum));

        count[stateNum].put(type,count[stateNum].get(type)+1);
    }

}

}

【问题讨论】:

  • 天哪。我不知道从哪里开始。我建议您使用调试器并将您的问题缩小到更具体的问题,然后编辑您的问题
  • 您在问题中键入的程序甚至无法编译。
  • 在询问错误时,请务必发布整个错误消息,包括任何异常的堆栈跟踪,并指出它正在谈论哪一行。
  • 为避免输入错误,请在将输入字符串解析为 int 的行周围使用 try/catch。
  • 怎么可能有人将其关闭为“太宽泛”?

标签: java hashmap


【解决方案1】:

您已经创建了一个HashMaps 数组:

@SuppressWarnings("unchecked")
static HashMap<String, Integer>[] count = new HashMap[50];

...但该数组中的地图未初始化。您的数组仅包含 50 个 null 引用。你必须先用 map 初始化它们,然后再给它添加任何东西。

if (count[stateNum] == null) {
    countNum[stateNum] = new HashMap<String, Integer();
}
count[stateNum].put(type,count[stateNum].get(type)+1);

另外,我建议您创建一个List&lt;Map&lt;String, Integer&gt;&gt;,而不是创建原始类型的数组。正如编译器所指出的那样,这确实不是类型安全的。

此外,您必须使用String[][][] 的事实清楚地表明您可能没有选择最好的数据结构,这可能会对您有所帮助。我猜,你几乎可以肯定在这里需要一个单独的课程。

【讨论】:

    【解决方案2】:

    您已经声明了HashMap 的数组,但没有对其进行初始化。

    static HashMap<String, Integer>[] count = new HashMap[50];
    

    使用前需要初始化HashMap的所有元素

    【讨论】:

      【解决方案3】:

      要使用 HashMap,您可以执行以下操作,并且永远不会出现空指针异常:

      static HashMap<String, Object[]>    hm  = new HashMap<String, Object[]>();
      
          public static void main( String[] args ) throws FileNotFoundException {
      
              File input = new File("./src/Problem1/stormdata_2013.csv");
              Scanner fileRead = new Scanner(input);
      
              while (fileRead.hasNextLine()) {
                  String currentLine = fileRead.nextLine();
                  Scanner lineScan = new Scanner(currentLine).useDelimiter(",");
      
                  // Dealing with unnecessary information
                  for (int i = 0; i < 8; i++) {
                      lineScan.next();
                  }
                  String state = lineScan.next().trim();
                  int stateNum = Integer.parseInt(lineScan.next());
                  lineScan.next();
                  String month = lineScan.next().trim();
                  String type = lineScan.next().trim();
      
                  Object[] obj = { stateNum, state, month };
                  hm.put(type, obj);
      
          }
      

      【讨论】:

        【解决方案4】:

        您甚至没有创建单个 HashMap。您为 50 个 HashMap 创建了一个数组。默认它包含 50 个空值,它必须包含 50 个新的 HashMap 对象。

        另外,写String[2][50][1000] 而不是String[50][1000][2] 以免浪费内存。 Explanation.

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-12
          • 1970-01-01
          • 1970-01-01
          • 2021-12-01
          • 2019-12-15
          • 1970-01-01
          相关资源
          最近更新 更多