我是Java和HBase的初学者,我想缩短我的代码,所以我在代码中使用了for循环。如果输入String stumber是" AAAA0000?0",结果应为" AAAA000000"," AAAA000010"," AAAA000020",&# 34; AAAA000030" ......" AAAA000090"
此原始代码效果很好:
List<Get> gets = new ArrayList<>();
for (String stumber : stumbersArr) {
if(stumber.charAt(8) == '?'){
get = new Get((stumber.replace(stumber.charAt(8), '0')).getBytes());
get.setMaxVersions(versions);
gets.add(get);
get = new Get((stumber.replace(stumber.charAt(8), '1')).getBytes());
get.setMaxVersions(versions);
gets.add(get);
get = new Get((stumber.replace(stumber.charAt(8), '2')).getBytes());
get.setMaxVersions(versions);
gets.add(get);
get = new Get((stumber.replace(stumber.charAt(8), '3')).getBytes());
get.setMaxVersions(versions);
gets.add(get);
get = new Get((stumber.replace(stumber.charAt(8), '4')).getBytes());
get.setMaxVersions(versions);
gets.add(get);
get = new Get((stumber.replace(stumber.charAt(8), '5')).getBytes());
get.setMaxVersions(versions);
gets.add(get);
get = new Get((stumber.replace(stumber.charAt(8), '6')).getBytes());
get.setMaxVersions(versions);
gets.add(get);
get = new Get((stumber.replace(stumber.charAt(8), '7')).getBytes());
get.setMaxVersions(versions);
gets.add(get);
get = new Get((stumber.replace(stumber.charAt(8), '8')).getBytes());
get.setMaxVersions(versions);
gets.add(get);
get = new Get((stumber.replace(stumber.charAt(8), '9')).getBytes());
get.setMaxVersions(versions);
gets.add(get);
}else{
get = new Get(stumber.trim().getBytes());
get.setMaxVersions(versions);
gets.add(get);
}
}
Result[] results = table.get(gets);
但是在用for-loop修改后,代码无法给出结果。为什么?
List<Get> gets = new ArrayList<>();
for (String stumber : stumbersArr) {
if(stumber.charAt(8) == '?'){
for (int i =0; i<10; i++){
get = new Get((stumber.replace(stumber.charAt(8), 'i')).getBytes());
get.setMaxVersions(versions);
gets.add(get);
}
}else{
get = new Get(stumber.trim().getBytes());
get.setMaxVersions(versions);
gets.add(get);
}
}
Result[] results = table.get(gets);
答案 0 :(得分:2)
您可以使用Character.forDigit()将int i转换为char,如:
get = new Get((stumber.replace(stumber.charAt(8), Character.forDigit(i, 10))).getBytes());
答案 1 :(得分:1)
内部for循环应该是:
get = new Get((stumber.replace(stumber.charAt(8), (char)(i + '0'))).getBytes());
什么使i
值为char
。