【问题标题】:I get null values while adding on the same index of an array在数组的相同索引上添加时我得到空值
【发布时间】:2015-07-23 09:43:31
【问题描述】:

嘿,我试图从字符串中识别数字并将它们转换为字符串数组,但是使用这段代码我得到的是:

IE: [null1, null2, exc..]

我不知道如何取出这个空值。
我也想找到一种方法来做到这一点,没有数组,因为我应该手动增加它的长度。

有什么建议吗?谢谢!

public class ProvaToArray {
    public static void main(String[] x) {
        String s = "1-2-3-4-lorem23ip567um1";
        String[] ris = toArray(s);
        System.out.println(Arrays.toString(ris)); //should printout  [1, 2, 3, 4, 23, 567, 1]
    }

    public static String[] toArray(String s){
        String[] l = new String[10];
        int count = 0;
        for(int i = 0; i < s.length() - 1; i++){
            if(estNumber(s.charAt(i) + "")){
                l[count] += "" + s.charAt(i);
                if(!estNumber(s.charAt(i+1) + "")){
                    count++;
                }
            }
            if(i+1 == s.length()-1){
                if(estNumber(s.charAt(i+1) + "")){
                    l[count] = "" + s.charAt(i+1);
                }
            }
        }
        return l;
    }

    public static boolean estNumber(String i){
        if(i.matches("^-?\\d+$"))
            return true;
        else
            return false;
    }
}

【问题讨论】:

    标签: java arrays indexing


    【解决方案1】:

    当你初始化你的字符串数组时:

    String[] l = new String[10];
    

    每个索引都包含 null。

    当您将一个字符串附加到该空值时,您会得到一个字符串“null”附加到您的字符串。

    如果你改变:

    l[count] += "" + s.charAt(i);
    

    if (l[count] == null)
        l[count] = "" + s.charAt(i);
    else
        l[count] += "" + s.charAt(i);
    

    您将摆脱“空”字符串。

    我看到您发布的代码作为答案。如果它不回答问题,则不应将其作为答案发布。您应该已将其添加到问题中。

    无论如何,您的问题是访问 String s 的第 i+2'th 个字符,这会在循环的最后一次迭代中导致 IndexOutOfBoundsException。 与其在同一迭代中检查i'th、i+1'th 和i+2'th 字符,不如在每次迭代中检查一个字符,并使用局部变量来保存当前数字。

    您的代码可能比现在简单得多:

    public static String[] toArray(String s)
    {
        ArrayList<String> m = new ArrayList<>();
        StringBuilder currentNumber = new StringBuilder();
        for(int i = 0; i < s.length(); i++) {
            if(Character.isDigit(s.charAt(i))) {
                currentNumber.append (s.charAt(i));
            } else {
                if (currentNumber.length() > 0) {
                    m.add(currentNumber.toString());
                    currentNumber.setLength(0);
                }
            }
        }
        if (currentNumber.length() > 0) {
            m.add(currentNumber.toString());
        }                    
        return m.toArray(new String[m.size()]);
    }
    

    这将为您使用的输入生成数组[1, 2, 3, 4, 23, 567, 1]

    【讨论】:

    • 非常感谢!但是,如果我不想使用 String[],而是使用 arrayList?我怎样才能摆脱这个?
    • @Benny 如果您使用 ArrayList,则必须调用 list.add("" + s.charAt(i))list.set(count,list.get(count) + s.charAt(i));,具体取决于您是否已将元素添加到 List 的 count 索引。你仍然需要一个条件。
    • 我真正的问题是将它添加到 arrayList 中的同一索引上。我尝试了设置,添加,但仍然无法管理它
    • @Benny list.set(count,list.get(count) + s.charAt(i)); 应该可以工作,只要list.size()&gt;count
    • 它抛出 IndexOutOfBoundExceptions。不明白为什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多