Kri*_*her 0 java regex arraylist
我有一个函数应该使用给定ArrayList<String>并将输出返回为ArrayList<Integer>. 问题是NullPointerException提出来的hourList.add(hourInt);,我不知道为什么。
public ArrayList<Integer> takeTime(ArrayList<String> logs) {
ArrayList<Integer> hourList=null;
Integer hourInt;
for(String line: logs) {
String[] matrix = line.split(" ");
String[] hour = matrix[3].split(":");
// System.out.print(hour[0]+"\n");
String s = hour[0].replaceFirst("^0+(?!$)", "");
hourInt = Integer.parseInt(s);
System.out.print(hourInt+",");
hourList.add(hourInt); // <--- NullPointerException here
}
return hourList;
}
Run Code Online (Sandbox Code Playgroud)
System.out.print(hourInt+",");:
0,0,0,0,1,1,2,3,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10
Run Code Online (Sandbox Code Playgroud)
您需要ArrayList在声明时构建您的:
ArrayList<Integer> hourList = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
否则hourList.add将给出 NPE,因为引用hourList为空。