public class test { public static void main(String[] args){ String str = "|DF|A3"; System.out.println(str.split("\\|").length); } } 这样输出就对了。
//以空串或者空串分隔字符串,实际上是分隔每一个字符
& -- 逻辑与 | -- 逻辑或 "a1b2cc1dd".split("|");这里的 | 其实是一个逻辑或运算符,我们把其当作字符串就好了。 &&-- 短路与 || -- 短路或
- String[] arr = "a1b2cc1dd".split("|");
- System.out.println(arr.length);
- for(String s:arr){
- System.out.println(s);
- }
- 结果:
- 10
- a
- 1
- b
- 2
- c
- c
- 1
- d
- d
上面延伸[|] 是什么东西?
"|DF|A|||".split("[|]").length = 32.
"||DF|A|||".split("[|]").length = 4
int i=0;
if(3>2 || (i++)>1) i=i+1;
System.out.println(i);
这段程序会打印出1,而不是打印出2。
因为在if的条件判断中,程序先判断第一个表达式3>2是否成立,结果3>2为真,那么按照逻辑来说,无论后面一个表达式(i++)>1是否成立,整个或表达式肯定为真,因此程序就不去执行判断后面一个表达式即(i++)>1了,所以这里i并没有自增1。然后程序执行到i=i+1,于是i变为1。最后打印出1。
3.main中一个str change方法中一个str注意区分两者
- public class Example{
- String str=new String("good");
- char[]ch={'a','b','c'};
- public static void main(String args[]){
- Example ex=new Example();
- ex.change(ex.str,ex.ch);
- System.out.print(ex.str+" and ");
- Sytem.out.print(ex.ch);
- }
- public void change(String str,char ch[]){
- str="test ok";
- ch[0]='g';
- }
- }
其结果是 good and gbc ,为啥 string 没改变而char 改变了呢??
原来是因为:
str是按值传递,所以在函数中对它的操作只生效于它的副本,与原字符串无关。 ch是按址传递,在函数中根据地址,可以直接对字符串进行操作。
str指向的是String类的引用,保存的是地址。当新的“test good ”产生的时候,会在内存中新建String对象,将地址复制给changes方法的str。而ch不会产生新的char数组,直接对原来的数组进行更改。
然后,当原来的程序改下后:
- public class StringArrTest {
- String str = new String("good");
- char[] ch = { 'a', 'b', 'c' };
- int[] arr = new int[] { 1, 2, 3 };
- public static void main(String[] args) {
- StringArrTest ex = new StringArrTest();
- ex.changes(ex.str, ex.ch, ex.arr);
- System.out.print(ex.str + " and ");
- System.out.print(ex.ch);
- System.out.print(ex.arr[0]);
- // System.out.println(ex.arr);
- }
- public void changes(String str, char ch[], int[] arr) {
- str = "test ok";
- System.out.println("chagne str:" + str);
- ch[0] = 'g';
- System.out.print("change ch :");
- System.out.println(ch);
- arr[0] = 4;
- System.out.print("change arr :");
- System.out.println(arr[0]);
- }
- }
可以看到,输出字符数组和整形数组是不一样的,字符数组会把所有字符输出来,那是因为System.out.print(a);此方法会自动给你解析你的数组,然后打印出来的。而当是整形数组的时候,就会打印出该数组的地址(当然是首地址)。
如果在将
System.out.print(ex.str + " and " +ex.ch);
ch也会打印出字符数组的地址,而不会解析数组,依次打印每个字符了。
- 顶
- 0
- 踩
- public class Example{
- String str=new String("good");
- char[]ch={'a','b','c'};
- public static void main(String args[]){
- Example ex=new Example();
- ex.change(ex.str,ex.ch);
- System.out.print(ex.str+" and ");
- Sytem.out.print(ex.ch);
- }
- public void change(String str,char ch[]){
- str="test ok";
- ch[0]='g';
- }
- }
其结果是 good and gbc ,为啥 string 没改变而char 改变了呢??
原来是因为:
str是按值传递,所以在函数中对它的操作只生效于它的副本,与原字符串无关。 ch是按址传递,在函数中根据地址,可以直接对字符串进行操作。
str指向的是String类的引用,保存的是地址。当新的“test good ”产生的时候,会在内存中新建String对象,将地址复制给changes方法的str。而ch不会产生新的char数组,直接对原来的数组进行更改。
然后,当原来的程序改下后:
- public class StringArrTest {
- String str = new String("good");
- char[] ch = { 'a', 'b', 'c' };
- int[] arr = new int[] { 1, 2, 3 };
- public static void main(String[] args) {
- StringArrTest ex = new StringArrTest();
- ex.changes(ex.str, ex.ch, ex.arr);
- System.out.print(ex.str + " and ");
- System.out.print(ex.ch);
- System.out.print(ex.arr[0]);
- // System.out.println(ex.arr);
- }
- public void changes(String str, char ch[], int[] arr) {
- str = "test ok";
- System.out.println("chagne str:" + str);
- ch[0] = 'g';
- System.out.print("change ch :");
- System.out.println(ch);
- arr[0] = 4;
- System.out.print("change arr :");
- System.out.println(arr[0]);
- }
- }
可以看到,输出字符数组和整形数组是不一样的,字符数组会把所有字符输出来,那是因为System.out.print(a);此方法会自动给你解析你的数组,然后打印出来的。而当是整形数组的时候,就会打印出该数组的地址(当然是首地址)。
如果在将
System.out.print(ex.str + " and " +ex.ch);
ch也会打印出字符数组的地址,而不会解析数组,依次打印每个字符了。