有这样的代码。
public class CSVtestclass {
public static void main(String[] args) throws IOException {
String FileName = "c:\\TestJava\\report.csv";
File file = new File(FileName);
try (BufferedReader read = new BufferedReader(new FileReader(file))) {
String reader;
String wordName;
String wordCount;
HashMap<String, int[]> stringIntegerHashMap = new HashMap<>();
read.readLine();
while ((reader = read.readLine()) != null) {
try {
String[] next = reader.split(";");
wordName = next[10];
wordCount= next[17];
wordCount = wordCount.replace("\"", "");
if (stringIntegerHashMap.containsKey(wordName)) {
int[] ints = stringIntegerHashMap.get(wordName);
ints[0]++;
ints[1] += Integer.parseInt(wordCount);
} else {
stringIntegerHashMap.put(wordName, new int[]{1, Integer.parseInt(wordCount)});
}
} catch (NumberFormatException e) {
System.out.println(e);
}
}
for (Map.Entry<String, int[]> pair : stringIntegerHashMap.entrySet()) {
int[] value = pair.getValue();
System.out.printf("%s zayavki: %d interakcii: %d%n", pair.getKey(), value[0], value[1]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
我只是无法理解数组 []ints 如何设法影响 stringIntegerHashMap 元素的结果???毕竟,他只是粗略地“为自己工作”。那些。它的结果没有分配到任何地方,但它设法做到了 - 即对地图中键的值求和。
` if (stringIntegerHashMap.containsKey(wordName)) {
int[] ints = stringIntegerHashMap.get(wordName);
ints[0]++;
ints[1] += Integer.parseInt(wordCount);
} else {
stringIntegerHashMap.put(wordName, new int[]{1, Integer.parseInt(wordCount)});
在这里,您在键下的字典中放置了一个新的数组引用
wordName在这里你可以得到它
wordName数组是引用类型,所以这里:
您使用对它的引用,字典中的引用指向它。
数组,甚至是基元,都已经是对象了。对象存储在堆上,只有对这些对象的引用存储在变量中。
因此这条线
array
ints指的是 中的同一个数组Map。对数组内部的任何更改ints都会更改堆上的数组本身。这些更改将反映在引用同一数组的所有变量中。