数组VS集合
集合类:提供了一种存储空间可变的存储模型,存储的数据容量可以改变。
e.g. ArrayList
ArrayList<E>
包: java.util
- 是一个可调整大小的数组的实现
- <E>是一种特殊的数据类型:泛型。(指该集合中存储的元素的数据类型)(集合中存储字符串:ArrayList<String>;若集合中存储对象:ArrayList<Student>)
ArrayList构造方法
- public ArrayList() —— 构造初始容量为空的集合对象;
默认创建容量为10的空集合,当集合容量满了时,每次扩容1.5倍。若在构建时直接设定容量大小则不扩容。
ArrayList arrayList = new ArrayList();
ArrayList<Integer> arrayList_ = new ArrayList<>(); //指定了集合存储的类型
- pubic ArrayList( int initialCapacity) —— 构造具有指定初始容量的空集合对象。
int size = 20;
ArrayList arrayList1 = new ArrayList(size);
- 指定类型,指定大小。
ArrayList<String> str_arr =new ArrayList<String>(20);
- public ArrayList(Collection<? extends E> c) —— 构造包含指定集合的元素的列表,按照它们由集合的迭代器返回的顺序。
Collection<Integer> collection=new ArrayList<Integer>();
collection.add(1);
collection.add(2);
collection.add(3);
ArrayList<Integer> c_arr = new ArrayList<Integer>(collection);
ArrayList——添加元素
- 在集合末尾添加:public boolean add(E e)
boolean ans = arrayList.add("1");
- 在集合指定位置添加: pulic void add(int index , E e)
arrayList.add(0,2); //无返回值
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/69100.html