java吧 关注:1,227,967贴子:12,688,193

String数组一个很奇怪的问题

只看楼主收藏回复

我首先创建了一个String字符串a1,然后用split加上没有任何内容的正则表达式返回了一个字符串数组a2,然后用if查找字符串数组里的内容,发现完全查找不到。开始以为是哪里出了错了,后来又试了一下直接创建一个字符串数组,然后直接给他赋值进去,然后查找,发现能查找到的。。
下面是源码 求分析谢谢
public class test{
public static void main(String[] args){
String a1 = "123,456,78,9";
String regex = "";
String a2[] = a1.split(regex);
System.out.println("a2.length=" + a2.length);
for(int i=0;i<a2.length;i++) System.out.print("a2[" + i+ "]"+"=" +a2[i] + ",");
System.out.println("" );
int comnum1 = 0;
for(int i=0;i<a2.length;i++)
if(a2[i]==",") comnum1++;
System.out.println(comnum1);
String[] a3 = new String[]{"1","2","3",",","4","5","6",",","7","8",",","9"};
System.out.println("a3.length=" + a3.length);
for(int i=0;i<a3.length;i++) System.out.print("a3[" + i+ "]"+"=" +a3[i] + ",");
System.out.println("" );
int comnum2 = 0;
for(int i=0;i<a3.length;i++)
if(a3[i]==",") comnum2++;
System.out.println(comnum2);
}
}


IP属地:广东1楼2017-02-22 19:04回复
    滴土采


    来自手机贴吧9楼2017-02-22 20:12
    回复
      split根据给的内容 返回一个分割好的字符串数组 第一个 根本无法分割 字符串是空的


      IP属地:福建来自iPhone客户端11楼2017-02-22 20:16
      回复
        还有 你for循环是执行一个语句还是一段代码块? 没大括号????


        IP属地:福建来自iPhone客户端12楼2017-02-22 20:18
        收起回复
          a2.length=12
          a2[0]=1,a2[1]=2,a2[2]=3,a2[3]=,,a2[4]=4,a2[5]=5,a2[6]=6,a2[7]=,,a2[8]=7,a2[9]=8,a2[10]=,,a2[11]=9,
          0
          a3.length=12
          a3[0]=1,a3[1]=2,a3[2]=3,a3[3]=,,a3[4]=4,a3[5]=5,a3[6]=6,a3[7]=,,a3[8]=7,a3[9]=8,a3[10]=,,a3[11]=9,
          3


          45楼2017-02-22 22:02
          回复
            equals()比较字符串


            46楼2017-02-22 22:05
            收起回复
              public static void main(String[] args) {
              String a1 = "123,456,78,9";
              String regex = "";
              String a2[] = a1.split(regex);
              System.out.println("a2.length=" + a2.length);
              for (int i = 0; i < a2.length; i++)
              System.out.print("a2[" + i + "]" + "=" + a2[i] + ",");
              System.out.println("");
              int comnum1 = 0;
              for (int i = 0; i < a2.length; i++)
              if (a2[i].equals(","))
              comnum1++;
              System.out.println(comnum1);
              String[] a3 = new String[] { "1", "2", "3", ",", "4", "5", "6", ",", "7", "8", ",", "9" };
              System.out.println("a3.length=" + a3.length);
              for (int i = 0; i < a3.length; i++)
              System.out.print("a3[" + i + "]" + "=" + a3[i] + ",");
              System.out.println("");
              int comnum2 = 0;
              for (int i = 0; i < a3.length; i++)
              if (a3[i].equals(","))
              comnum2++;
              System.out.println(comnum2);
              }


              47楼2017-02-22 22:07
              收起回复
                感觉用equals判断是否等于逗号


                IP属地:河北来自iPhone客户端59楼2017-02-22 22:47
                回复
                  室惭盼


                  来自手机贴吧62楼2017-02-22 23:01
                  回复