Java学习路程之Arrays中的方法、基本数据类型包装类、StringBuffer和collection

一.Arrays中的方法
1.排序Arrays.sort()

public class Day12 {
    public static void main(String[] args) {
        String[] array ={"nba","abc","cba","aaaa","z","qq"};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
        //输出:[aaaa, abc, cba, nba, qq, z]
    }
}

2.二分查找

public class Day12 {
    public static void main(String[] args) {
        //返回元素在数组中的索引
        //如果数组中没有该元素,返回该元素应该数组中的位置+1并取负值
        String[] array ={1, 2, 3, 4, 5, 6};
        int index = Arrays.binarySearch(array, 4); 
        System.out.println(Arrays.toString(array));
        //输出:3
    }
}

二.基本数据类型的包装类
1.基本数据类型的包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
基本数据类型的包装类增加了成员变量和成员方法

public class Day12 {
    public static void main(String[] args) {
        //创建一个Integer对象
        Integer integer = new Integer(10);
        System.out.println(integer);//输出:10
        //将字符串转化为Integer对象,必须是数字格式的字符串才能转换
        Integer integer1 = new Integer("123");
        System.out.println(integer1);//输出:123
        //将Integer类型的转化为 int类型
        int num = integer1.intValue();
        System.out.println(num);//输出:123
        //将int类型转化为Integer类型
        Integer integer2 = num.valueOf();
        System.out.println(integer2);//输出:123
        2.自动装箱和自动拆箱
        //声明一个Integer对象
        //自动装箱
        Integer i = 10;//声明时系统自动调用了valueOf()这个方法
        System.out.println(i);//10
        //自动拆箱
        int num1 = i + 10;//系统自动调用了intValue()这个方法
        System.out.println(num1);//输出:20
    }
}

三.StringBuffer
1.StringBuffer和StringBuilder
StringBuffer 线程是安全的意味着会耗费系统资源
StringBuilder 线程不安全的
StringBuffer和StringBuilder的方法和用法完全一致
2.拼接

public class Day12 {
    public static void main(String[] args) {
        //创建StringBuffer对象
        StringBuffer sb = new StringBuffer();
        //获取初始容量 
        System.out.println(sb.capacity());
        //拼接(核心方法),在原有基础上拼接
        sb.append("hello").append("world");
        System.out.println(sb);//输出:helloworld
        //获取字符串长度
        System.out.println(sb.length());//10
        //StringBuffer转化成字符串
        String str = sb.toString();
        System.out.println(str);//helloworld
    }
}

3.插入 修改

public class Day12 {
    public static void main(String[] args) {
        //创建StringBuffer对象
        StringBuffer sb = new StringBuffer();
        sb.append("hello").append("world");
        System.out.println(sb);//输出:helloworld
        //插入字符
        sb.insert(1, 'a');
        System.out.println(sb);//输出:haelloworld
        //修改字符
        sb.setCharAt(1, 'i');
        System.out.println(sb);//输出:hielloworld
    }
}

4.删除

public class Day12 {
    public static void main(String[] args) {
        //创建StringBuffer对象
        StringBuffer sb = new StringBuffer();
        sb.append("hello").append("world");
        System.out.println(sb);//输出:helloworld
        sb.delete(2,4);//结束坐标可以越界
        System.out.println(sb);//heoworld
        //删除索引处的字符
        sb.delectCharAt(3);
        System.out.println(sb);//heoorld
        //获取stringBuffer中字符
        char c = sb.charAt(2);
        System.out.println(c);//输出:o
    }
}

5.反转

public class Day12 {
    public static void main(String[] args) {
    //反转 键盘输入一个字符串 将字符串反转
        System.out.println("请输入一个字符串");
        Scanner sc = new Scanner();
        String str = sc.next();
        //将其内容初始化为指定的字符串内容
        StringBuffer sb = new StringBuffer(str);
        sb.reverse();
        System.out.println(sb);
    }
}

6.替换

public class Day12 {
    public static void main(String[] args) {
        StringBuffer sb  = new StringBuffer();
         sb.append("helloworld");
         sb.replace(0,5, "hi");
         System.out.println(sb);//输出:hiworld
    }
}

7.字符数组转stringBuffer

public class Day12 {
    public static void main(String[] args) {
        char[] array = {'a', 'b', 'c'};
        //将字符数组转化为字符串
        String str = new String(array);
        //将字符串转化为stringBuffer
        StringBuffer sb = new StringBuffer(str);
        System.out.println(sb);//abc
    }
}

练习1:
/*
* 需求
* 把int[] array = new int[]{1,2,3,4};
* 输出上面的 [1, 2, 3, 4]
* 要求:使用两种拼接方法(String 和 StringBuffer
* */

public class Day12 {
    public static void main(String[] args) {
        方法1:
        int[] array = new int[]{1,2,3,4};
        String str = "[";
        for(int i = 0; i < array.length; i++){
            if(i == array.length - 1){
                str = str + array[i] + "]";
            }else{
                str = str + array[i] + ",";
            }
        }
        System.out.println(string);
        方法2:
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for(int i = 0; i < array.length; i++){
            if(i == array.length - 1){
                sb.append(array[i]).append( "]");           }else{
                sb.append(array[i]).append(",");
            }
        }
        System.out.println(sb);
    }
}

练习2:
/*
* 创建一个能保存三个学生的数组
* 遍历数组 打印每一个学生信息
* 学生类(name age)
* 私有化成员变量 有参无参
* set/get方法
* toString方法
*/

public class Day12 {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("李四", "20");
        students[1] = new Student("张三", "18");
        students[2] = new Student("王五", "16");
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i]);
        }
    }
}
class Student{
    private String name;
    private String age;
    //构造方法
    public Student() {

    }

    public Student(String name, String age) {
        this.name = name;
        this.age = age;
    }

    //set/get方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
    //toString方法重写
    public String toString() {
        String string  = "名字" + name + "年龄" + age;
        return string;
    }

}

四.Collection集合
数组弊端:长度一经确定就不可改变;只能保存相同数据类型的元素
集合:长度可以改变,可以保存任意类型的元素
注意:只能保存引用数据类型

public class Day12 {
    public static void main(String[] args) {
        1.添加
        Collection Collection = new ArrayList();
        //向集合中添加基本数据类型时,系统会自动装箱,实际上保存到集合中的是Integer对象
        //抽象方法要适用所有的子类,返回值是给set使用的
        boolean b1 = collection.add(1);
        boolean b2 = collection.add("b");
        boolean b3 = collection.add("c");
        boolean b4 = collection.add("d");
        System.out.println(collection);//1,b,c,d
        2.获取集合的长度
        Collection Collection = new ArrayList();
        boolean b1 = collection.add("a");
        boolean b2 = collection.add("b");
        boolean b3 = collection.add("c");
        int num = collection.size();
        System.out.println(num);//3
        3.判断是否包含某个元素
        boolean b1 = collection.contains("s");
        System.out.println(b1);//false
        4.从集合中删除一个元素
        boolean b2 = collection.remove("c");
        System.out.println(b2);//a,b
        5. 判断集合 是否为空
        boolean b3 = collection.isEmpty();
        System.out.println(b3);//false
        6.清空集合
        collection.clear();
        System.out.println(collection);
        7.集合转数组
        boolean b1 = collection.add("a");
        boolean b2 = collection.add("b");
        boolean b3 = collection.add("c");
        Object obj = collection.toArray();
        for (int i = 0; i < obj.length; i++) {
            System.out.println(obj[i]);
        }
    }
}

练习:
/*
* 多态形式创建一个集合
* 添加三个学生对象
* 遍历集合
* 只打印学生姓名(其他的不打印)
*/


public class Day12 {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        collection.add( new Student("李四", "20"));
        collection.add( new Student("张三", "18"));
        collection.add(  new Student("王五", "16"));
        Object[] object = collection.ToArray();
for(int i= 0; i < object.length; i++){
    //将数组中的每一个元素强转
    Student std = (Student)object[i];//向下转型
    System.out.println(std.getName());
}
    }
}

5.权限

权限修饰符:
public          公共的
protected       受保护的
default         默认的(不加修饰符就是默认的)
private         私人的
在不同情况下的权限:yes能够访问,no不能访问
                    本类     同包类      同包子类     不同包类      不同包子类 
public              yes     yes         yes          yes           yes
protected           yes     yes         yes          no            yes              
default             yes     yes         yes          no            no
private             yes     no          no           no            no

猜你喜欢

转载自blog.csdn.net/l710820742/article/details/82389929