ArrayList
ArrayList<E>:
-
可调整大小的数组实现
-
<E>是一种特殊的数据类型,泛型
-
在出现E的地方我们使用引用数据类型替换即可,如ArrayList<String>,ArrayList<Student>

package com.cheng.arraylist;
/*
ArrayList构造方法:
public ArrayList(); 创建一个空的集合对象
ArrayList添加方法:
public boolean add(E e);将指定的元素追加到此集合的末尾
public void add(int index,E element) 在此集合中的指定位置插入指定的元素
*/
import java.util.ArrayList;
public class Demo01 {
public static void main(String[] args) {
//public ArrayList(); 创建一个空的集合对象
//ArrayList<String> array = new ArrayList<>(); 还有另外的写法如下行所示
ArrayList<String> array = new ArrayList<String>();
//public boolean add(E e); 添加方法
//System.out.println(array.add("hello"));
//输出 true true 表示添加成功
// Array:[hello]
array.add("hello");
array.add("world");
array.add("nihao");
//public void add(int index,E element) 在此集合中的指定位置插入指定的元素
array.add(3,"niu");//Array:[hello, world, nihao,niu] 把索引位置的元素往后挤了一下
//此时集合元素为4个。编号为 0 1 2 3
//若是插入index为5还可以插入,要是为6或者更大就不行了 集合索引越界IndexOutOfBoundsException
//输出集合
System.out.println("Array:"+array);//
}
}

浙公网安备 33010602011771号