为什么是“"在数组的第 0 个索引中执行没有分隔符的 split() 吗? [英] why a "" in the 0th index of an array on perfoaming a split() w/o delimiters?

查看:21
本文介绍了为什么是“"在数组的第 0 个索引中执行没有分隔符的 split() 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static void main(String[] args) {
    // TODO Auto-generated method stub

    String str="aaabbddaabbcc";
    String[] str2=str.split("");
    String pointer=str2[0];
    int count=0;
    String finalStr="";
    for(String str132:str2)
    {
        if(str132.equalsIgnoreCase(pointer))
        {
            ++count;
        }
        else
        {

            finalStr+=str132+count;
            count=0;
            pointer=str132;
            ++count;
        }
    }
    System.out.println(finalStr);
}

在执行 str.split("") 时,为什么我在 str2 数组的第 0 个索引中得到一个 ""?

On performing a str.split(""), why am I getting a "" in the 0th index of the str2 array?

推荐答案

为什么我在 str2 数组的第 0 个索引中得到一个 ""?

why am i getting a "" in the 0th index of the str2 array?

因为您使用的分隔符在此处匹配:

Because the delimiter you use has matched here:

 aaaabbddaabbcc
^

由于 .split() 收集的部分是在进入字符串时走过"的,所以这里收集的是空字符串.

Since .split() collects the parts is has "walked by" when it progresses into the string, here it collects the empty string.

还要注意,由于定界符为空,为了避免死循环,在下一次迭代时,.split()会前移一个字符,然后再开始搜索.

Note also that since the delimiter is empty, in order to avoid infinite loops, at the next iteration, .split() will forward one character before starting to search again.

这篇关于为什么是“"在数组的第 0 个索引中执行没有分隔符的 split() 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆