【ArrayList集合】
生活随笔
收集整理的這篇文章主要介紹了
【ArrayList集合】
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.ArrayList
1.1ArrayList類概述
-
什么是集合
? 提供一種存儲空間可變的存儲模型,存儲的數(shù)據(jù)容量可以發(fā)生改變
-
ArrayList集合的特點
? 底層是數(shù)組實現(xiàn)的,長度可以變化
-
泛型的使用
? 用于約束集合中存儲元素的數(shù)據(jù)類型
1.2ArrayList類常用方法
1.2.1構(gòu)造方法
| public ArrayList() | 創(chuàng)建一個空的集合對象 |
1.2.2成員方法
| public boolean remove(Object o) | 刪除指定的元素,返回刪除是否成功 |
| public E remove(int index) | 刪除指定索引處的元素,返回被刪除的元素 |
| public E set(int index,E element) | 修改指定索引處的元素,返回被修改的元素 |
| public E get(int index) | 返回指定索引處的元素 |
| public int size() | 返回集合中的元素的個數(shù) |
| public boolean add(E e) | 將指定的元素追加到此集合的末尾 |
| public void add(int index,E element) | 在此集合中的指定位置插入指定的元素 |
1.2.3示例代碼
public class ArrayListDemo02 {public static void main(String[] args) {//創(chuàng)建集合ArrayList<String> array = new ArrayList<String>();//添加元素array.add("hello");array.add("world");array.add("java");//public boolean remove(Object o):刪除指定的元素,返回刪除是否成功 // System.out.println(array.remove("world")); // System.out.println(array.remove("javaee"));//public E remove(int index):刪除指定索引處的元素,返回被刪除的元素 // System.out.println(array.remove(1));//IndexOutOfBoundsException // System.out.println(array.remove(3));//public E set(int index,E element):修改指定索引處的元素,返回被修改的元素 // System.out.println(array.set(1,"javaee"));//IndexOutOfBoundsException // System.out.println(array.set(3,"javaee"));//public E get(int index):返回指定索引處的元素 // System.out.println(array.get(0)); // System.out.println(array.get(1)); // System.out.println(array.get(2));//System.out.println(array.get(3)); //ArrayIndexOutOfBoundsException 索引越界//public int size():返回集合中的元素的個數(shù)System.out.println(array.size());//輸出集合System.out.println("array:" + array);} }總結(jié)
以上是生活随笔為你收集整理的【ArrayList集合】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NHapi
- 下一篇: hive 插入数据映射到hbase_hi