• java ArrayList


    一、产生原因

    1、Array是定长

    例子:

     1 package cn.wt.day07.Demon03AL;
     2 
     3 public class Person {
     4     private String name;
     5     private int age;
     6 
     7     public Person() {
     8     }
     9 
    10     public Person(String name, int age) {
    11         this.name = name;
    12         this.age = age;
    13     }
    14 
    15     public String getName() {
    16         return name;
    17     }
    18 
    19     public void setName(String name) {
    20         this.name = name;
    21     }
    22 
    23     public int getAge() {
    24         return age;
    25     }
    26 
    27     public void setAge(int age) {
    28         this.age = age;
    29     }
    30 }
    Person
     1 package cn.wt.day07.Demon03AL;
     2 
     3 public class Demon02Per {
     4     public static void main(String[] args) {
     5         Person[] per = new Person[3];
     6         per[0] = new Person("tom", 24);
     7         per[1] = new Person("路飞", 18);
     8         per[2] = new Person("索隆", 20);
     9 
    10         System.out.println(per[2].getName());
    11         for (int i = 0; i < per.length; i++) {
    12             // 地址
    13             System.out.println(per[i]);
    14         }
    15     }
    16 }
    Demon02Per

    二、注意

    1、ArrayList<E>

    E指的是泛型,即list中数据的数据类型

    2、E只能使用引用数据类型

    3、基本数据类型要想使用,需要使用包装类型

    4、ArrayList<E> 的默认值是 [] 与 python 的list一样

    三、基本数据类型和包装类型

    byte      Byte

    short       Short 

    int         Integer (拼写可能不对)

    long           Long

    float      Float

    double       Double  

    char        Character (拼写可能不对)

    boolean     Boolean

    四、常用方法

    1、add(内容) 添加

    2、get(index) 获取

    3、remove(index) 删除

    4、size() 计算list的长度

    例子

    引用数据类型

    package cn.wt.day07.Demon03AL;
    
    import java.util.ArrayList;
    
    public class Demon01 {
        public static void main(String[] args) {
            ArrayList<String> strLst = new ArrayList<>();
            strLst.add("路飞");
            strLst.add("索隆");
            strLst.add("娜美");
            System.out.println(strLst);
            System.out.println("索引为2的值为" + strLst.get(2));
            strLst.remove(0);
            System.out.println(strLst);
            System.out.println(strLst.size());
        }
    }

    基本数据类型 借助 包装类型

    package cn.wt.day07.Demon03AL;
    
    import java.util.ArrayList;
    
    public class Demon02 {
        public static void main(String[] args) {
            ArrayList<Integer> intLst = new ArrayList<>();
            // 打印空值
            System.out.println(intLst);
            // 增加
            intLst.add(10);
            intLst.add(15);
            intLst.add(5);
            intLst.add(30);
            System.out.println(intLst);
            // 获取
            System.out.println(intLst.get(1));
            // 删除
            System.out.println(intLst.remove(1));
            // 遍历
            System.out.println("===========================");
            for (int i = 0; i < intLst.size(); i++) {
                System.out.println(intLst.get(i));
            }
        }
    }

    练习

    1、将整型基本数据类型储存在ArrayList中,并遍历出结果

     1 package cn.wt.day07.Demon04;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Random;
     5 
     6 // 要求 生成 6个 1 到 66 的随机数字,保存到集合并遍历
     7 public class Demon01 {
     8     public static void main(String[] args) {
     9         Random ran = new Random();
    10         ArrayList<Integer> intLst = new ArrayList<>();
    11         for (int i = 0; i < 6; i++) {
    12             int randNum = ran.nextInt(66) + 1;
    13             intLst.add(randNum);
    14         }
    15         for (int i = 0; i < intLst.size(); i++) {
    16             System.out.println(intLst.get(i));
    17         }
    18     }
    19 }
    View Code

    2、自定义4个学生类,并保存到ArrayList中,并遍历

    注意:ArrayList中储存的是 Student 对象的内存地址

     1 package cn.wt.day07.Demon04;
     2 
     3 public class Student {
     4     private String name;
     5     private int age;
     6 
     7     public Student() {
     8     }
     9 
    10     public Student(String name, int age) {
    11         this.name = name;
    12         this.age = age;
    13     }
    14 
    15     public String getName() {
    16         return name;
    17     }
    18 
    19     public void setName(String name) {
    20         this.name = name;
    21     }
    22 
    23     public int getAge() {
    24         return age;
    25     }
    26 
    27     public void setAge(int age) {
    28         this.age = age;
    29     }
    30 }
    Student.java
     1 package cn.wt.day07.Demon04;
     2 
     3 import java.util.ArrayList;
     4 
     5 // 自定义4个学生类,并保存到ArrayList中,并遍历
     6 public class Demon02 {
     7     public static void main(String[] args) {
     8         Student stu1 = new Student("东邪", 78);
     9         Student stu2 = new Student("西毒", 69);
    10         Student stu3 = new Student("南帝", 59);
    11         Student stu4 = new Student("北丐", 47);
    12 
    13         ArrayList<Student> stuLst = new ArrayList<>();
    14         stuLst.add(stu1);
    15         stuLst.add(stu2);
    16         stuLst.add(stu3);
    17         stuLst.add(stu4);
    18 
    19         for (int i = 0; i < stuLst.size(); i++) {
    20             System.out.println("姓名:" + stuLst.get(i).getName() + "年龄:" + stuLst.get(i).getAge());
    21         }
    22     }
    23 }
    Demon02.java

    3、定义打印集合的方法, 将ArrayList中的元素打印出来,打印结果{元素@元素@元素}

     1 package cn.wt.day07.Demon04;
     2 
     3 import java.util.ArrayList;
     4 
     5 // 定义打印集合的方法, 将ArrayList中的元素打印出来,打印结果{元素@元素@元素}
     6 public class Demon03 {
     7     public static void main(String[] args) {
     8         ArrayList<String> strLst = new ArrayList<>();
     9         strLst.add("tom");
    10         strLst.add("jack");
    11         strLst.add("rose");
    12         strLst.add("joker");
    13         isPrint(strLst);
    14     }
    15 
    16     public static void isPrint(ArrayList<String> strLst){
    17         System.out.print("{");
    18         for (int i = 0; i < strLst.size(); i++) {
    19             if(i == strLst.size() - 1){
    20                 System.out.print(strLst.get(i));
    21             } else {
    22                 System.out.print(strLst.get(i) + "@");
    23             }
    24         }
    25         System.out.print("}");
    26     }
    27 }
    Demon04.java

    4、用大集合随机存放20个整数,在取其中的奇数存在小集合中,使用自定义方法

     1 package cn.wt.day07.Demon04;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Random;
     5 
     6 // 用大集合随机存放20个整数,在取其中的奇数存在小集合中,使用自定义方法
     7 public class Demon04 {
     8     public static void main(String[] args) {
     9         ArrayList<Integer> bigLst = new ArrayList<>();
    10         Random ran = new Random();
    11         for (int i = 0; i < 20; i++) {
    12             bigLst.add(ran.nextInt(100));
    13         }
    14         ArrayList<Integer> res = isChoice(bigLst);
    15         System.out.println(res);
    16     }
    17     public static ArrayList<Integer> isChoice(ArrayList<Integer> isLst){
    18         ArrayList<Integer> smallLst = new ArrayList<>();
    19         for (int i = 0; i < isLst.size(); i++) {
    20             if(isLst.get(i) % 2 ==1){
    21                 smallLst.add(isLst.get(i));
    22             }
    23         }
    24         return smallLst;
    25     }
    26 }
    Demon05.java
  • 相关阅读:
    MySQL自定义函数 1418报错
    MySQL存储过程查询
    MySQL存储过程---游标
    MySQL存储过程---流程控制(循环)
    MySQL存储过程---流程控制(分支)
    设计模式——单例模式
    准备写一个 四川票务网的 检测票自动买汽车票功能,结果登录不上悲伤,继续研究
    python批量下载微信好友头像,微信头像批量下载
    arduino 522样本中文注释
    zabbix服务的部署
  • 原文地址:https://www.cnblogs.com/wt7018/p/12189841.html
Copyright © 2020-2023  润新知