HashMap/Hashtable 在 for 循环中不返回 int 作为键值
HashMap/Hashtable not returning int as key value in for loop
当我试图在 for 循环中访问 map.get(c) 时(如版本 2 所示),它 returns 空值并将上限设置为空,导致空 -指针异常。另一方面,如果我创建 end 变量并为其分配 map.get(c) 值(如版本 1 所示),它工作正常。那么,你能解释一下为什么吗?
版本 1:工作得很好
int count=0;
int st=0;
string s = "abcabcbb";
Hashtable<Character, Integer> map = new Hashtable<Character, Integer>();
char[] str = s.toCharArray();
for(int i=0; i<str.length; i++){
char c = str[i];
if(map.get(c)==null){
map.put(c, i);
if(count < map.get(c) - st + 1){
count = map.get(c) - st + 1;
};
}
else {
int end = map.get(c); // End variable --> returns int value as expected
for(int j=st; j<=end; j++){
map.remove(str[j]);
st = j+1;
}
map.put(c,i);
}
}
System.out.println(count);
版本 2:提供空指针异常
int count=0;
int st=0;
string s = "abcabcbb";
Hashtable<Character, Integer> map = new Hashtable<Character, Integer>();
char[] str = s.toCharArray();
for(int i=0; i<str.length; i++){
char c = str[i];
if(map.get(c)==null){
map.put(c, i);
if(count < map.get(c) - st + 1){
count = map.get(c) - st + 1;
};
}
else {
//int end = map.get(c); // End variable commented
for(int j=st; j<=map.get(c); j++){ // replaced end w map.get(c) --> returns null instead of int
map.remove(str[j]);
st = j+1;
}
map.put(c,i);
}
}
System.out.println(count);
提前感谢您的帮助!
洛汗.
for 循环运行直到它的条件不满足(在你的例子中,直到 j <= map.get(c)
是 false
)。此条件也未缓存,如下面代码的输出所示:
public static void main(String[] args) {
for (int i = 0; i < getCondition(); i++) {
}
}
private static int getCondition() {
System.out.println("Test");
return 3;
}
输出:
Test
Test
Test
Test
因此,for 循环的每次迭代都会调用 map.get(c)
。如果您碰巧从 map
中删除了带有键 c
的条目,那么从 Map#get
返回的值是 null
,这就是导致 NullPointerException
.
当我试图在 for 循环中访问 map.get(c) 时(如版本 2 所示),它 returns 空值并将上限设置为空,导致空 -指针异常。另一方面,如果我创建 end 变量并为其分配 map.get(c) 值(如版本 1 所示),它工作正常。那么,你能解释一下为什么吗?
版本 1:工作得很好
int count=0;
int st=0;
string s = "abcabcbb";
Hashtable<Character, Integer> map = new Hashtable<Character, Integer>();
char[] str = s.toCharArray();
for(int i=0; i<str.length; i++){
char c = str[i];
if(map.get(c)==null){
map.put(c, i);
if(count < map.get(c) - st + 1){
count = map.get(c) - st + 1;
};
}
else {
int end = map.get(c); // End variable --> returns int value as expected
for(int j=st; j<=end; j++){
map.remove(str[j]);
st = j+1;
}
map.put(c,i);
}
}
System.out.println(count);
版本 2:提供空指针异常
int count=0;
int st=0;
string s = "abcabcbb";
Hashtable<Character, Integer> map = new Hashtable<Character, Integer>();
char[] str = s.toCharArray();
for(int i=0; i<str.length; i++){
char c = str[i];
if(map.get(c)==null){
map.put(c, i);
if(count < map.get(c) - st + 1){
count = map.get(c) - st + 1;
};
}
else {
//int end = map.get(c); // End variable commented
for(int j=st; j<=map.get(c); j++){ // replaced end w map.get(c) --> returns null instead of int
map.remove(str[j]);
st = j+1;
}
map.put(c,i);
}
}
System.out.println(count);
提前感谢您的帮助! 洛汗.
for 循环运行直到它的条件不满足(在你的例子中,直到 j <= map.get(c)
是 false
)。此条件也未缓存,如下面代码的输出所示:
public static void main(String[] args) {
for (int i = 0; i < getCondition(); i++) {
}
}
private static int getCondition() {
System.out.println("Test");
return 3;
}
输出:
Test
Test
Test
Test
因此,for 循环的每次迭代都会调用 map.get(c)
。如果您碰巧从 map
中删除了带有键 c
的条目,那么从 Map#get
返回的值是 null
,这就是导致 NullPointerException
.