java学习-集合

为什么有集合?

自动扩容

数组:长度固定,可以存基本数据类型和引用数据类型

集合:长度可变,可以存引用数据类型,基本数据类型的话需要包装类

ArrayList

java 复制代码
public class studentTest {
    public static void main(String[] args) {
        ArrayList<String> list=new ArrayList<>();
        boolean res=list.add("aaa");
        System.out.println(res); //true
        System.out.println(list);//[aaa]
        list.add("bbb");
        list.add("cccc");
        System.out.println(list); //[aaa, bbb, cccc]
        //删除
        boolean res1=list.remove("aaa");
        System.out.println(res1); //true
        System.out.println(list);//[bbb, cccc]
        //根据索引删除,返回被删除元素
        String str=list.remove(0);
        System.out.println(str);//bbb
        //修改,返回旧值
        String res2=list.set(0,"000");
        System.out.println(res2);//cccc
        System.out.println(list);//[000]
        //查询
        String str3=list.get(0);
        System.out.println(str3);//000
        //获取长度
        System.out.println(list.size());//1

    }


}

案例 创建集合,添加元素,遍历

java 复制代码
public class studentTest {
    public static void main(String[] args) {
        ArrayList<String> list=new ArrayList<>();
        list.add("bhjhsd");
        list.add("dwshv");
        list.add("fs2552");
        System.out.print("[");
        for (int i = 0; i <list.size() ; i++) {
            if(i==list.size()-1){
                System.out.println(list.get(i)+"]");
            }else{
                System.out.print(list.get(i)+", ");
            }
        }

    }


}

案例 定义一个集合 添加数字,遍历。

包装类

java 复制代码
public class studentTest {
    public static void main(String[] args) {
        ArrayList<Integer> list=new ArrayList<>();
        list.add(1);
        list.add(3);
        list.add(5);
        System.out.print("[");
        for (int i = 0; i <list.size() ; i++) {
            if(i==list.size()-1){
                System.out.println(list.get(i)+"]");
            }else{
                System.out.print(list.get(i)+", ");
            }
        }
//[1, 3, 5]
    }


}

案例 添加学生类对象

java 复制代码
//存储学生信息
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;
    }


}
java 复制代码
public class studentTest {
    public static void main(String[] args) {
        ArrayList<Student> list=new ArrayList<>();
        Student s1=new Student("lisa",15);
        Student s2=new Student("bob",18);
        Student s3=new Student("anna",17);
        list.add(s1);
        list.add(s2);
        list.add(s3);
        for (int i = 0; i < list.size(); i++) {
            Student stu=list.get(i);
            System.out.println(stu.getName()+", "+stu.getAge());
        }
    }
//lisa, 15
//bob, 18
//anna, 17

}

键盘录入学生信息:

java 复制代码
public class studentTest {
    public static void main(String[] args) {
        ArrayList<Student> list=new ArrayList<>();
        Scanner sc=new Scanner(System.in);
        for (int i = 0; i <3 ; i++) {
            Student stu =new Student();
            System.out.println("输入学生姓名");
            String name=sc.next();
            System.out.println("输入学生年龄");
            int age=sc.nextInt();
            stu.setAge(age);
            stu.setName(name);
            list.add(stu);

        }
        for (int i = 0; i <list.size() ; i++) {
            Student s=list.get(i);
            System.out.println(s.getName()+", "+s.getAge());
        }
//hbkjsd, 12
//dv, 15
//dvg, 13
    }


}

案例 添加用户对象并判断是否存在

java 复制代码
public class User {

    private String id;
    private String username;
    private String password;


    public User() {
    }

    public User(String id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
java 复制代码
public class userTest {
    public static void main(String[] args) {
        ArrayList<User> list =new ArrayList<>();
        User u1=new User("001","zhangsan","123456");
        User u2=new User("002","lisi","12354526");
        User u3=new User("003","wangwu","123dfssdf6");
        list.add(u1);
        list.add(u2);
        list.add(u3);
        System.out.println(contain(list,"002"));

    }
    public static boolean contain(ArrayList<User> list,String id){
        for (int i = 0; i <list.size() ; i++) {
            boolean res=list.get(i).getId().equals(id);
            if(res){
                return true;
            }
        }
        return false;
    }


}

更改,存在返回索引,不存在返回-1

java 复制代码
public class userTest {
    public static void main(String[] args) {
        ArrayList<User> list =new ArrayList<>();
        User u1=new User("001","zhangsan","123456");
        User u2=new User("002","lisi","12354526");
        User u3=new User("003","wangwu","123dfssdf6");
        list.add(u1);
        list.add(u2);
        list.add(u3);
        System.out.println(contain(list,"002"));

    }
    public static int contain(ArrayList<User> list,String id){
        for (int i = 0; i <list.size() ; i++) {
            boolean res=list.get(i).getId().equals(id);
            if(res){
                return i;
            }
        }
        return -1;
    }


}

案例 创建手机对象,低于价格的返回

java 复制代码
//把价格低于3000的返回
public class phoneTest {
    public static void main(String[] args) {
        ArrayList<Phone> list =new ArrayList<>();
        Phone p1=new Phone("xiaomi",1000);
        Phone p2=new Phone("pingguo",8000);
        Phone p3=new Phone("xchuizi",2999);
        list.add(p1);
        list.add(p2);
        list.add(p3);
        ArrayList<Phone> reslist=getPhoneInfo(list);
        for (int i = 0; i < reslist.size(); i++) {
            Phone p=reslist.get(i);
            System.out.println(p.getBrand()+", "+p.getPrice());
        }

    }
    public static ArrayList<Phone> getPhoneInfo(ArrayList<Phone> list){
        ArrayList<Phone> reslist=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Phone p=list.get(i);
            int price=p.getPrice();
            if(price<3000){
                reslist.add(p);
            }
        }
        return reslist;
    }


}
相关推荐
A懿轩A36 分钟前
C/C++ 数据结构与算法【数组】 数组详细解析【日常学习,考研必备】带图+详细代码
c语言·数据结构·c++·学习·考研·算法·数组
南宫生8 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
sanguine__9 小时前
Web APIs学习 (操作DOM BOM)
学习
数据的世界0111 小时前
.NET开发人员学习书籍推荐
学习·.net
四口鲸鱼爱吃盐11 小时前
CVPR2024 | 通过集成渐近正态分布学习实现强可迁移对抗攻击
学习
OopspoO13 小时前
qcow2镜像大小压缩
学习·性能优化
A懿轩A14 小时前
C/C++ 数据结构与算法【栈和队列】 栈+队列详细解析【日常学习,考研必备】带图+详细代码
c语言·数据结构·c++·学习·考研·算法·栈和队列
居居飒14 小时前
Android学习(四)-Kotlin编程语言-for循环
android·学习·kotlin
kkflash314 小时前
提升专业素养的实用指南
学习·职场和发展
1 9 J15 小时前
数据结构 C/C++(实验五:图)
c语言·数据结构·c++·学习·算法