为什么""在阵列上perfoaming分割第0指数()W / O分隔符? [英] why a "" in the 0th index of an array on perfoaming a split() w/o delimiters?

查看:153
本文介绍了为什么""在阵列上perfoaming分割第0指数()W / O分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公共静态无效的主要(字串[] args){
    // TODO自动生成方法存根    字符串str =aaabbddaabbcc;
    的String [] = str2的str.split();
    字符串指针= STR2 [0];
    诠释计数= 0;
    串finalStr =;
    对于(字符串str132:STR2)
    {
        如果(str132.equalsIgnoreCase(指针))
        {
            ++计数;
        }
        其他
        {            finalStr + = str132 +计数;
            计数= 0;
            指针= str132;
            ++计数;
        }
    }
    的System.out.println(finalStr);
}

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


解决方案

  

为什么我在STR2数组的第0指数得到一个?


由于您使用的分隔符在这里匹配:

  aaaabbddaabbcc
^

由于 .split()收集部分是已当它进步入字符串,这里收集空字符串。

擦身而过

还要注意的是,因为分隔符是空的,为了避免无限循环,下一轮迭代, .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);
}

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

解决方案

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

Because the delimiter you use has matched here:

 aaaabbddaabbcc
^

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

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.

这篇关于为什么""在阵列上perfoaming分割第0指数()W / O分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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