在我的例子中for()循环有什么问题?

ssgvzors  于 2021-06-09  发布在  Hbase
关注(0)|答案(2)|浏览(321)

我是java和hbase的初学者,我想缩短代码,所以在代码中使用for循环。如果输入字符串stumber为“aaaa0000?0”,则结果应为“aaaa000000”、“aaaa000010”、“aaaa000020”、“aaaa000030”…“aaaaaa000090”
此源代码运行良好:

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循环修改之后,代码没有给出结果。为什么?

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);
31moq8wy

31moq8wy1#

for循环的内部应该是:

get = new Get((stumber.replace(stumber.charAt(8), (char)(i + '0'))).getBytes());

什么给了你 i 价值观 char .

roqulrg3

roqulrg32#

可以使用character.fordigit()将int i转换为char,如下所示:

get = new Get((stumber.replace(stumber.charAt(8), Character.forDigit(i, 10))).getBytes());

相关问题