学习JAVA第十二天——ArrayList

数组的长度不可以发生改变
但是ArrayList集合的长度是可以随意改变的
对于ArrayList来说,有一个尖括号代表泛型
泛型:也就是装在集合当中的所有元素,全都是统一的什么类型
注意:泛型只能是引用类型,不能是基本类型,如果希望向泛型中写基本数据类型,可以使用包装类
基本数据类型 包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
从JDK1.5开始,支持自动装箱、自动拆箱
自动装箱:基本类型–>包装类型
自动拆箱:包装类型–>基本类型

public class ArrayListDome {
    public static void main(String[] args) {
        //创建一个ArrayList集合,集合的名称是list,里面装的全都是String字符串类型的数据
        //备注,从JDK1.7+开始,右侧的尖括号内部可以不写内容,但是<>本身还是要写的

        ArrayList<String> list = new ArrayList<>();
        System.out.println(list);
        // 对于ArrayList,直接打印得到的是内容
        //       如果内容为空得到的是[]
        //向集合内加内容需要用到add
        list.add("张三");
        list.add("李四");
        System.out.println(list);//[张三, 李四]
    }
}

ArrayList当中的常用方法有:
public boolean add(E e):向集合当中添加元素,参数的类型和泛型一致
备注:对于ArrayList 集合来说,add添加动作一定成功,所以返回值可用可不用
但是对于其他集合来说,add添加动作不一定成功

public E get(int index):从集合当中获取元素,参数是索引编号,返回值就是对应位置的元素
public E remove(int index):从集合当中删除元素,参数索引编号,返回值就是被删除掉的元素
public int size():获取集合的尺寸长度,返回值是集合中包含的元素个数

public class ArrayListMethod {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        System.out.println(list);
        //向集合添加元素
        boolean success = list.add("王五");
        System.out.println(list);
        System.out.println("是否成功"+success);
        list.add("李四");
        list.add("张三");
        //从集合中拿数据
        String name1 = list.get(1);
        System.out.println(name1);
        //从集合删除元素,Remov
        String remove = list.remove(2);
        System.out.println("被删除的人是:"+remove);
        //统计集合中元素个数
        int size = list.size();
        System.out.println("集合长度是:"+size);
        //集合的遍历
        for(int i = 0 ;i<list.size();i++){
            System.out.print(list.get(i));
        }
        //[]
        //[王五]
        //是否成功true
        //李四
        //被删除的人是:张三
        //集合长度是:2
        //王五李四
    }
}

生成6个1~33之间的随机整数,添加到集合,并遍历集合

public class ArrayListDemo01 {
    public static void main(String[] args) {
        //创建Random对象
        Random r = new Random();
        //创建ArrayList对象
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 6 ; i++) {
            list.add(r.nextInt(33)+1);
        }
        for (int i = 0; i < 6; i++) {
            System.out.println(list.get(i));
        }
        //3
        //14
        //32
        //9
        //21
        //9
    }
}

定义三个学生对象添加到集合并遍历集合

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;
    }
}
public class ArrayListDemo02 {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Student one = new Student("张三",20);
        Student two = new Student("李四",30);
        Student three = new Student("王五",40);
        list.add(one);
        list.add(two);
        list.add(three);
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getName()+"  " +stu.getAge());
        }
       //张三  20
        //李四  30
        //王五  40
    }
}

定义以指定格式打印集合的方法(ArrayList类型作为参数),使用{}扩起来集合,使用@分割每个元素
格式参照{元素@元素@元素}

public class ArrayListDemo03 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        ShowArrayList(list);
    }
    public static void ShowArrayList(ArrayList<String> list){
        System.out.print("{");
        for (int i = 0; i < list.size(); i++) {
            if(i==list.size()-1){
                System.out.print(list.get(i)+"}");
            }else {
                System.out.print(list.get(i)+"@");
            }
        }
    }
}
//{张三@李四@王五}

用一个大集合存入20个随机数字,然后筛选其中的偶数元素,放到小集合当中。
要求使用自定义的方法来实现筛选

public class ArrayListDemo04 {
    public static void main(String[] args) {
        ArrayList<Integer> biglist = new ArrayList<>();
        Random r = new Random();
        for (int i = 0; i < 20 ; i++) {
            int a = r.nextInt(100)+1;
            biglist.add(a);
        }
        ArrayList<Integer> samllList = SamllList(biglist);
        System.out.println(samllList);
        //[4, 8, 14, 28, 40, 68, 82, 98, 28, 50, 12, 96, 78]
    }
    public static ArrayList<Integer> SamllList(ArrayList<Integer> biglist){
        ArrayList<Integer> samllList = new ArrayList<>();
        for (int i = 0; i < biglist.size(); i++) {
            int a = biglist.get(i);
            if(a % 2 == 0){
                samllList.add(a);
            }
        }
        return samllList;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值