day07【StringBuilder练习和ArrayList集合】课上

1.StringBuilder练习_拼接字符串(课下完成)

package com.itheima.sh.stringbuilder_01;


/*
    需求:
    ​	定义一个方法,把 int 数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法,
    ​	并在控制台输出结果。例如,数组为int[] arr = {1,2,3}; ,执行方法后的输出结果为:"[1, 2, 3]"
    步骤:
    1.定义一个int类型的数组 int[] arr = {1,2,3};
    2.在主方法中调用将数组拼接为字符串的方法,并接收返回的字符串
    3.自定义方法将数组中的数据拼接为字符串 int[] arr = {1,2,3};
    4.在定义方法中创建StringBuilder类的对象用来拼接数据
    5.向StringBuilder中添加一个数据 "["
    6.遍历数组取出数组中的每个数据 1  ---》
        "[1, 2, "  所以:前两个数据都是添加: "数据, "
        最后一个数据:"3]"--->"数据]"
        总结:
            最后一个数据添加缓冲区:"数据]"
            不是最后一个数据添加缓冲区: "数据, "
    7.判断取出的数据是否是最后一个数据:
        如果取出的数据的索引等于最大索引(数组长度-1):说明是最后一个数据:最后一个数据添加缓冲区:"数据]"
        如果取出的数据的索引不等于最大索引(数组长度-1):说明不是最后一个数据: 不是最后一个数据添加缓冲区: "数据, "
        int[] arr = {1,2,3};
        [1, 2, 3]
    8.将字符串缓冲区转换为String
    9.将转换后的String对象返回给调用者
    10.在主方法中输出接收的结果

 */
public class StringBuilderTest01 {
    public static void main(String[] args) {
        // 1.定义一个int类型的数组
        int[] arr = {1, 2, 3};
        //2.在主方法中调用将数组拼接为字符串的方法,并接收返回的字符串
        String s = arrayToString(arr);
        //10.在主方法中输出接收的结果
        System.out.println("s = " + s);
    }

    //3.自定义方法将数组中的数据拼接为字符串 int[] arr = {1,2,3};
    private static String arrayToString(int[] arr) {
        //4.在定义方法中创建StringBuilder类的对象用来拼接数据
        StringBuilder sb = new StringBuilder();
        //5.向StringBuilder中添加一个数据 "["
        sb.append("[");
        // 6.遍历数组取出数组中的每个数据
        for (int i = 0; i < arr.length; i++) {
            int num = arr[i];
            //7.判断取出的数据是否是最后一个数据:
            if(i == arr.length -1){
                // 如果取出的数据的索引等于最大索引(数组长度-1):说明是最后一个数据:最后一个数据添加缓冲区:"数据]"
                sb.append(num).append("]");
            }else{
                //如果取出的数据的索引不等于最大索引(数组长度-1):说明不是最后一个数据: 不是最后一个数据添加缓冲区: "数据, "
                sb.append(num).append(", ");
            }
        }
        //8.将字符串缓冲区转换为String
//        String s = sb.toString();
        String s = new String(sb);//不推荐使用
        //9.将转换后的String对象返回给调用者
        return s;
    }
}

2.使用对象数组存储自定义类的对象(理解)

需求:使用数组存储三个自定义类Student的对象

package com.itheima.sh.demo_02;

/*
    需求:使用数组存储三个自定义类Student的对象
    步骤:
    1.创建Student类
    2.定义数组存储自定义类Student的对象,长度是3
        数组定义:
            int[] arr=new int[3]; 存储int类型
            String[] arr=new String[3]; 存储String类型的字符串
            Student[] arr=new Student[3]; 存储Student类型的对象
    3.创建三个学生对象
    4.将学生对象添加到数组中
    5.遍历数组取出每个学生对象
    6.使用学生对象调用方法输出姓名和年龄

 */
public class Test01 {
    public static void main(String[] args) {
        // 2.定义数组存储自定义类Student的对象,长度是3
        Student[] arr = new Student[3];
        //3.创建三个学生对象
        Student s1 = new Student("张三", 19);
        Student s2 = new Student("李四", 20);
        Student s3 = new Student("王五", 20);
        //4.将学生对象添加到数组中
        arr[0] = s1;
        arr[1] = s2;
        arr[2] = s3;
        //5.遍历数组取出每个学生对象
        for (int i = 0; i < arr.length; i++) {
            Student s = arr[i];
            // 6.使用学生对象调用方法输出姓名和年龄
            System.out.println(s.getName() + "---" + s.getAge());
        }
    }
}

小结:我们以前都是使用数组存储基本类型数据,其实也使用数组存储引用类型就是字符串,在java中数组既可以存储基本类型也可以存储引用数据类型。

3.ArrayList集合(必须掌握)

1.集合

集合属于一种容器,专门用来存储引用数据类型的数据,不能存储基本数据类型数据。长度可变,可以存储任意类型的引用数据类型。

我们今天先学习第一种集合:ArrayList集合。

总结:

数组和集合区别:

1.数组长度固定不变的,集合长度是可变的
2.数组可以存储基本数据类型也可以存储引用数据类型,集合只能存储引用数据类型,不能存储基本数据类型
3.数组存储的数据类型单一,集合可以存储不同的引用数据类型

2.ArrayList集合介绍

1.ArrayList集合是集合的一种,底层使用数组存储数据。他属于类。可以创建这个类的对象,并且使用对象调用方法。

2.该类位于java.util 包,需要导包。

3.可以存储null

3.ArrayList构造方法

ArrayList() 构造一个初始容量为 10 的空列表。

说明:ArrayList 在类的后面多了一个,表示泛型,和 接口 方法一起使用。

如果加了泛型,表示集合中只能存储泛型表示的具体数据类型,这里的E表示Element元素的缩写。

 ArrayList<String> list = new ArrayList<String>(); 说明该集合只能存储String类型
 ArrayList<Student> list = new ArrayList<Student>(); 说明该集合只能存储Student类型
 jdk7后可以简化写法:
     ArrayList<Student> list = new ArrayList<>(); 说明该集合只能存储Student类型

4.ArrayList方法

  • 添加方法 获取长度 获取数据方法

    package com.itheima.sh.arraylist_03;
    
    import java.util.ArrayList;
    
    /*
        方法:
            1.boolean add(E e) 将指定的元素添加到此列表的尾部。
                参数:
                    e:表示要添加到集合中的数据 E类型由创建ArrayList对象时确定
                返回值:对于ArrayList集合返回都是true
            2.public int size()返回集合中的元素的个数
            3.public E   get(int   index)返回指定索引处的元素
                    参数:index:表示索引,从0开始
                    返回值:表示取出的数据
     */
    public class ArrayListDemo01 {
        public static void main(String[] args) {
            //ArrayList() 构造一个初始容量为 10 的空列表。
            //这里泛型的具体数据类型是String,那么这个集合只能存储String类的对象
            ArrayList<String> list = new ArrayList<>();
            //向集合中添加数据
            list.add(null);
            list.add("蓉蓉");
            list.add("蓉蓉");
            list.add("骚喆");
            list.add("璐璐");
            boolean boo = list.add("pgone");
    //        System.out.println("boo = " + boo);
            //输出list
            //list = [蓉蓉, 骚喆, 璐璐, pgone] toString
    //        System.out.println("list = " + list);//"[蓉蓉, 骚喆, 璐璐, pgone]"
            //2.public int size()返回集合中的元素的个数
           /* int size = list.size();
            System.out.println("size = " + size);//4
            //3.public E   get(int   index)返回指定索引处的元素
            String s = list.get(0);//蓉蓉
            System.out.println("s = " + s);
            System.out.println(list.get(1));//骚喆
            System.out.println(list.get(2));//璐璐
            System.out.println(list.get(3));//pgone
            //IndexOutOfBoundsException 索引越界异常 因为最大索引是3 4超出了最大索引
            System.out.println(list.get(4));*/
            //使用for循环遍历上述集合中的数据
    //        for(int i=0;i<集合长度;i++){//i表示索引
            for (int i = 0; i < list.size(); i++) {//i表示索引  list.size() 表示集合长度
                //使用get方法结合索引获取集合数据
                String s = list.get(i);
                System.out.println("s = " + s);
            }
        }
    }
    
    

    小结:

    ​ 1.boolean add(E e) 将指定的元素添加到此列表的尾部。
    ​ 参数:
    ​ e:表示要添加到集合中的数据 E类型由创建ArrayList对象时确定
    ​ 返回值:对于ArrayList集合返回都是true
    ​ 2.**public int size()**返回集合中的元素的个数
    ​ 3.**public E get(int index)**返回指定索引处的元素
    ​ 参数:index:表示索引,从0开始
    ​ 返回值:表示取出的数据

    ​ 4.查看源码:按住ctrl键,然后左键点击方法名即可

    ​ 5.Arraylist集合可以存储重复的数据和null

  • 其余方法

    方法名说明
    public boolean remove(Object o)删除指定的元素,返回删除是否成功
    public E remove(int index)删除指定索引处的元素,返回被删除的元素
    public E set(int index,E element)修改指定索引处的元素,返回被修改的元素
    public void add(int index,E element)在此集合中的指定位置插入指定的元素
package com.itheima.sh.arraylist_03;

import java.util.ArrayList;

/*
    1.public boolean remove(Object o)删除指定的元素,返回删除是否成功
            参数:表示要删除的元素
            返回值:删除成功是true 失败是false
    2.public E   remove(int   index)删除指定索引处的元素,返回被删除的元素
            参数:index 要删除元素的对应的索引值
            返回:被删除的元素
    3.public E   set(int index,E   element)修改指定索引处的元素,返回被修改的元素
        参数:
            index:被修改的元素索引
            element:新的元素
        返回值:旧的元素
    4.public void   add(int index,E   element)在此集合中的指定位置插入指定的元素
                参数:
                    index:向哪个索引位置添加元素
                    element:要添加单位元素
 */
public class ArrayListDemo02 {
    public static void main(String[] args) {
        //创建集合对象
        ArrayList<String> list = new ArrayList<>();
        //添加数据
        list.add("柳岩");
        list.add("柳岩");
        list.add("大鹏");
        list.add("杨幂");
        list.add("冰冰");
        //删除柳岩:  1.public boolean remove(Object o)删除指定的元素,返回删除是否成功4
        /*boolean boo = list.remove("柳岩");
        System.out.println("boo = " + boo);*/
        /*
             2.public E   remove(int   index)删除指定索引处的元素,返回被删除的元素
                参数:index 要删除元素的对应的索引值
                返回:被删除的元素
         */
//        String s1 = list.remove(1);//柳岩
//        String s1 = list.remove(2);//大鹏
//        System.out.println("被删除的元素:" + s1);
        /*
             3.public E   set(int index,E   element)修改指定索引处的元素,返回被修改的元素
            参数:
                index:被修改的元素索引
                element:新的元素
            返回值:旧的元素
         */
        //将冰冰修改为赵丽颖
//        String s2 = list.set(4, "赵丽颖");
//        System.out.println("旧的元素:"+s2);
        /*
             4.public void   add(int index,E   element)在此集合中的指定位置插入指定的元素
                参数:
                    index:向哪个索引位置添加元素
                    element:要添加单位元素
         */
        list.add(1, "锁哥");
        //遍历集合 list.fori遍历集合快捷键
        for (int i = 0; i < list.size(); i++) {
            //获取数据
            String s = list.get(i);
            System.out.println("s = " + s);
        }

    }
}

小结:

​ 1.public boolean remove(Object o)删除指定的元素,返回删除是否成功
​ 参数:表示要删除的元素
​ 返回值:删除成功是true 失败是false
​ 2.public E remove(int index)删除指定索引处的元素,返回被删除的元素
​ 参数:index 要删除元素的对应的索引值
​ 返回:被删除的元素
​ 3.public E set(int index,E element)修改指定索引处的元素,返回被修改的元素
​ 参数:
​ index:被修改的元素索引
​ element:新的元素
​ 返回值:旧的元素
​ 4.public void add(int index,E element)在此集合中的指定位置插入指定的元素
​ 参数:
​ index:向哪个索引位置添加元素
​ element:要添加单位元素

  • 扩展方法:

    package com.itheima.sh.arraylist_03;
    
    import java.util.ArrayList;
    
    /*
        1.void clear() 清空集合
        2.boolean contains(Object o) 如果此列表中包含指定的元素,则返回 true。
        3.boolean isEmpty() 如果此列表中没有元素,则返回 true
        4. Object[] toArray() 把集合中的数据放到Object类型数组
     */
    public class ArrayListDemo03 {
        public static void main(String[] args) {
            //创建集合对象
            ArrayList<String> list = new ArrayList<>();
            //添加数据
            list.add("柳岩");
            list.add("柳岩");
            list.add("大鹏");
            list.add("杨幂");
            list.add("冰冰");
    
    
            //遍历集合 list.fori遍历集合快捷键
           /* for (int i = 0; i < list.size(); i++) {
                //获取数据
                String s = list.get(i);
                System.out.println("s = " + s);
            }*/
            //清空集合
    //        list.clear();
    //        //调用toString()
    //        System.out.println(list);
            //2.boolean contains(Object o) 如果此列表中包含指定的元素,则返回 true。
    //        System.out.println(list.contains("大鹏1"));
            //3.boolean isEmpty() 如果此列表中没有元素,则返回 true
    //        System.out.println(list.isEmpty());//集合被清空了,那么集合没有数据,所以这里是true
            //4. Object[] toArray() 把集合中的数据放到Object类型数组
            Object[] arr = list.toArray();
            //遍历数组
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
        }
    }
    
    

    小结:

    ​ 1.void clear() 清空集合
    ​ 2.boolean contains(Object o) 如果此列表中包含指定的元素,则返回 true。
    ​ 3.boolean isEmpty() 如果此列表中没有元素,则返回 true

    4. Object[] toArray() 把集合中的数据放到Object类型数组
    

ArrayList集合中的方法总结:

方法名说明
public boolean remove(Object o)删除指定的元素,返回删除是否成功
public E remove(int index)删除指定索引处的元素,返回被删除的元素
public E set(int index,E element)修改指定索引处的元素,返回被修改的元素
public E get(int index)返回指定索引处的元素
public int size()返回集合中的元素的个数
public boolean add(E e)将指定的元素追加到此集合的末尾
public void add(int index,E element)在此集合中的指定位置插入指定的元素
void clear()清空集合
boolean contains(Object o)如果此列表中包含指定的元素,则返回 true。
boolean isEmpty()如果此列表中没有元素,则返回 true
Object[] toArray()把集合中的数据放到Object类型数组

5.ArrayList集合存储自定义类的学生对象(课下必须完成)

package com.itheima.sh.demo_02;
// 1.创建Student类
public class Student {
    private String name;
    private int age;

    public Student() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

   /* @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }*/
}

package com.itheima.sh.arraylist_04;

import com.itheima.sh.demo_02.Student;

import java.util.ArrayList;

/*
    ArrayList集合存储自定义类的学生对象
    步骤:
    1.创建学生类Student
    2.创建学生对象
    3.创建集合类ArrayList对象 泛型  ArrayList<Student> list = new ArrayList<Student>();
    4.将上述的学生对象添加到集合中
    5.遍历集合取出每个学生对象
    6.使用学生对象调用方法获取姓名和年龄并输出
 */
public class Test01 {
    public static void main(String[] args) {
        // 2.创建学生对象
        Student s1 = new Student("王泽萌", 25);
        Student s2 = new Student("宁宇", 20);
        Student s3 = new Student("李博", 25);
        Student s4 = new Student("李博", 25);
        // 3.创建集合类ArrayList对象 泛型
        ArrayList<Student> list = new ArrayList<>();
        //4.将上述的学生对象添加到集合中
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        //5.遍历集合取出每个学生对象 alt+/
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            //6.使用学生对象调用方法获取姓名和年龄并输出
            System.out.println(s.getName()+"---"+s.getAge());
        }
    }
}

小结:

1.使用集合存储自定义Student类型创建集合对象时指定泛型是Student

ArrayList<Student> list = new ArrayList<>();

2.集合使用步骤:

​ 1)创建集合对象

​ 2)向集合中添加数据

​ 3)遍历集合取出集合中的数据,然后继续后续操作

6.ArrayList集合存储自定义类的学生对象的升级版本(课下必须完成)

案例需求

创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合。

学生的姓名和年龄来自于键盘录入

代码演示:

package com.itheima.sh.arraylist_04;

import com.itheima.sh.demo_02.Student;

import java.util.ArrayList;
import java.util.Scanner;

/*
   需求:
    创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合。
    学生的姓名和年龄来自于键盘录入
    步骤:
    1.创建学生Student类
    2.创建集合对象,泛型是Student类
    3.调用方法专门用来获取键盘录入的数据,第2步创建的集合作为方法参数传递
    4.在自定义方法中创建键盘录入的对象和获取录入的姓名和年龄
        Scanner sc = new Scanner(System.in);
        sc.next();//姓名
        sc.nextInt();获取年龄
    5.将录入的姓名和年龄放到学生对象中
    6.将学生对象放到集合中
    7.遍历集合

 */
public class Test02 {
    public static void main(String[] args) {
        //2.创建集合对象,泛型是Student类
        ArrayList<Student> list = new ArrayList<>();
        //3.调用方法专门用来获取键盘录入的数据,第2步创建的集合作为方法参数传递
        addStudent(list);
        addStudent(list);
        addStudent(list);
        //7.遍历集合
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            //输出姓名和年龄
            System.out.println(s.getName()+"---"+s.getAge());
        }
    }
    //自定义方法添加学生
    //ArrayList<Student> list = list new ArrayList<>();
    public static void addStudent(ArrayList<Student> list){
        // 4.在自定义方法中创建键盘录入的对象和获取录入的姓名和年龄
        Scanner sc = new Scanner(System.in);
        //获取姓名
        System.out.println("请输入姓名:");
        String name = sc.next();
        //获取年龄
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        // 5.将录入的姓名和年龄放到学生对象中
//        Student s = new Student(name, age);
        Student s = new Student();
        s.setName(name);
        s.setAge(age);
        //6.将学生对象放到集合中
        list.add(s);
    }
}

小结:由于addStudent方法传递的是list属于 ArrayList类型,所以形参位置是 ArrayList类型

7.使用ArrayList集合存储基本数据类型的数据

说明:集合只能存储引用数据类型,不能存储基本数据类型的数据。如果要想将基本数据类型的数据存储到集合中,我们需要使用基本数据类型的对应的包装类。

基本数据类型有四类八种,对应的包装类也是四类八种。

基本数据类型: byte short int     long  float double char      boolean
    包装类:  Byte Short Integer Long  Float Double Character Boolean

代码演示:

package com.itheima.sh.arraylist_04;
import java.util.ArrayList;
public class Test03 {
    public static void main(String[] args) {
        /*
            基本数据类型: byte short int     long  float double char      boolean
            包装类:      Byte Short Integer Long  Float Double Character Boolean
         */
        //创建集合对象
        ArrayList<Integer> list = new ArrayList<>();
        //添加数据
        list.add(10);
        list.add(20);
        list.add(20);
        //遍历
        for (int i = 0; i < list.size(); i++) {
            Integer x = list.get(i);
            System.out.println(x);
        }
    }
}

小结:

1.包装类: Byte Short Integer Long Float Double Character Boolean

2.集合存储基本类型的数据必须使用包装类:

 ArrayList<Integer> list = new ArrayList<>();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

娃娃 哈哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值