java入门笔记5

String类常用方法 

package com.bjsxttest2;
public class SH {
    public static void main(String[] args) {
        String s2="abcdefg";
        String s3="abcDEFG";
        String s4="def";
        System.out.println(s2.charAt(0));//数组名.charAt(int):返回字符串下标为int的元素
        System.out.println(s2.charAt(s2.length()-1));//取字符串最后一个字符
        System.out.println(s2.equalsIgnoreCase(s3));//true。数组名.equalsIgnoreCase(数组名)是忽略大小写判断字符串是否相等。

        //从开头到结尾查找,返回所找到的第一个子字符串的索引位置,未找到返回-1
        System.out.println(s2.indexOf(s4));//结果为:3
        System.out.println(s2.indexOf("qqq"));//结果为:-1

        //从末尾开始找,返回找到的第一个子字符串的索引位置,未找到返回-1
        System.out.println("abcdefgdefg".lastIndexOf("defg"));//结果为:7

        //字符串的替换
        String  s5="abcdbcd".replace('d','D');
        System.out.println(s5);
        String  s6="abcdbcd".replace("cd","HELLO");
        System.out.println(s6);


        System.out.println("sxt,i love,u".startsWith("sxt")); //true
        System.out.println("st,i love u".endsWith("sxt"));//false

        //截取子字符串
        String s7="abcdefghijklmnopq".substring(6);//从第6个到最后
        System.out.println(s7);//ghijklmnopq
        String s8="abcdefghijklmnopq".substring(6,11);  //从6~(11-1)
        System.out.println(s8);//ghijk

        System.out.println("abcDE".toUpperCase());//小写变大写
        System.out.println("abcDDD".toLowerCase());//大写变小写

        //String trim()是删除字符串首尾的全部空格
        String s9="  a b  ";//长度7
        String s10=s9.trim();//长度为3
        System.out.println(s10.length());
        System.out.println(s10);

        //String是不可变字符序列。所有的替换、截取子字符串、去空格、转换大小等都是生成了新的字符串
        System.out.println(s9.replace(" ",""));//去掉全部空格




    }
}

for-each循环

注意:

一般用于读取元素的值

仅适用于遍历,不涉及有关索引(下标)的操作。

格式:for(数组类型 temp:数组名) 

package com.bjsxttest2;
public class SH {
    public static void main(String[] args) {
        String[] s={"aa","bb","cc"};
        for(String temp:s)
        {
            System.out.println(temp);
        }
    }
}

数组的拷贝

System类里也包含一个static void arraycopy(object src(称之原数组),int srcpos(拷贝位置),object dest(称之目标数组),int destpos(目标位置),int length(拷贝元素个数))方法,该方法可以将src数组里的元素赋值给dest数组的元素,其中srcpos指定从src数组的第几个元素开始赋值,length参数指定将src数组的多少个元素赋值给dest数组的元素。

例如:以下代码为起始位置为s1第二个元素(下标为1),目标位置为s2的第3个元素(下标为2),拷贝个数为3。

输出结果为:阿里 百度 京东

package com.bjsxttest2;
public class SH {
    public static void main(String[] args) {
        String[] s1={"腾讯","阿里","百度","京东","淘宝",};
        String[] s2=new String[5];
        System.arraycopy(s1,1,s2,2,s1.length-2);
        for (int i=2;i<s2.length;i++)
            System.out.printf("%s ",s2[i]);
    }
}

java.util.Arrays类

1.输出数组(Arrays.toString())。

注意:此处的Arrays.toString()方法是Arrays的静态方法。

package com.bjsxttest2;
import  java.util.Arrays;
public class SH {
    public static void main(String[] args) {
        int[] a={100,200,300};
        System.out.println(Arrays.toString(a));
    }
}

输出结果为:[100,200,300] 

 2.对数组进行排序。(Arrays.sort()

package com.bjsxttest2;
import  java.util.Arrays;
public class SH {
    public static void main(String[] args) {
        int[] a={2,4,3,5,7,9,6,1,8};
        Arrays.sort(a);
        System.out.println(Arrays.toString (a));
    }
}

输出结果为:[1,2,3,4,5,6,7,8,9] 

3.实现二分法查找法(Arrays.binarySearch())。

 注意:一定要先排序后查找,未找到返回一个负数。

package com.bjsxttest2;
import  java.util.Arrays;
public class SH {
    public static void main(String[] args) {
        int[] a={2,4,3,5,7,9,6,1,8};
        Arrays.sort(a);//进行二分法时一定要先排序
        System.out.println(Arrays.toString(a));
        System.out.println("该元素的索引为"+Arrays.binarySearch(a,6));//查找数组a中6元素的位置在哪里
    }
}

 

4.对数组进行填充(Arrays.fill())。 

注意:所改范围包头不包尾即[2,4],2的位置元素改变,4的位置元素不改变。

package com.bjsxttest2;
import  java.util.Arrays;
public class SH {
    public static void main(String[] args) {
        int[] a={2,4,9,6,1,8};
        Arrays.fill(a,2,4,100);//将2~4索引的位置改成100
        System.out.println(Arrays.toString(a));
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值