java API中提供的ArrayList的常用方法
来源:互联网 发布:mac口红铁锈红 编辑:程序博客网 时间:2024/06/04 20:10
根据java1.6 的API整理一下Arraylist的几个常用方法。
三个构造函数
1.public ArrayList(int initialCapacity);
构造一个具有指定初始容量的空列表
2.pubilc ArrayList();
构造一个初始容量为10的空列表
3.public ArrayList(Collection<> c)
构造一个包含指定collection的元素的列表。
若collection为null,会抛出NullPointerException
其他常用方法
4.trimToSize
public void trimToSize();
将此ArrayList的容量调整为列表的当前大小。应用程序可以使用此操作来最小化ArrayList 实例的存储量。
5.size
public int size();
返回此列表中的元素数。
6.isEmpty
public boolean isEmpty();
如果此列表中没有元素,则返回 true
7.contains
public boolean contains(Object o);
如果此列表中包含指定的元素,则返回 true
8.indexOf
public int indexOf(Object o)
返回此列表中首次出现的指定元素的索引,或如果此列表不包含元素,则返回 -1。
9.lastIndexOf
public int lastIndexOf(Object o)
返回此列表中最后一次出现的指定元素的索引,或如果此列表不包含索引,则返回 -1。
10.toArray
public Object[] toArray();
按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组。
由于此列表不维护对返回数组的任何引用,,因而它将是“安全的”。(换句话说,此方法必须分配一个新的数组)。因此,调用者可以自由地修改返回的数组。
此方法担当基于数组的 API 和基于 collection 的 API 之间的桥梁。
11.get
public E get(int index);
返回此列表中指定位置上的元素
12.set
public E set(int index, E element);
用指定的元素替代此列表中指定位置上的元素。返回值为以前位于该指定位置上的元素
13.add
public boolean add(E element);
将指定的元素添加到此列表的尾部。添加成功返回true
14.add
public void add(int index, E element)
将指定的元素插入此列表中的指定位置。向右移动当前位于该位置的元素(如果有)以及所有后续元素(将其索引加 1)。
15.remove
public E remove(int index)
移除此列表中指定位置的元素,返回从列表中移除的元素
16.remove
public boolean remove(Object o)
移除此列表中首次出现的指定元素(如果存在)。如果列表不包含此元素,则列表不做改动。
17.clear
public void clear()
移除此列表中的所有元素。此调用返回后,列表将为空
18.addAll
public boolean addAll(Collection c)
按照指定 collection 的迭代器所返回的元素顺序,将该 collection 中的所有元素添加到此列表的尾部
19.addAll
public boolean addAll(int index, Collection c)
从指定的位置开始,将指定 collection 中的所有元素插入到此列表中。
20.removeRange
protected void removeRange(int fromIndex, int endIndex);
移除列表中索引在 fromIndex(包括)和toIndex(不包括)之间的所有元素。向左移动所有后续元素(减小其索引)。此调用将列表缩短了 (toIndex - fromIndex) 个元素。(如果toIndex==fromIndex,则此操作无效。)
package collection;import java.util.ArrayList;import java.util.List;import java.util.Random;public class arraylistTest {public static Random random = new Random();public static char[] charArray = {};public static void main(String [] args){List<String> list = new ArrayList<String>();//往list加入10个随机字符串for(int i = 0;i < 10 ;i++){list.add(arraylistTest.GenRandomString(5));}//往list中插入一个固定字符串list.add("abc");//1.判断arraylist是否为空boolean isEmpty = list.isEmpty();System.out.println("arraylist是否为空:" + isEmpty);//2.返回列表中的元素数int size = list.size();System.out.println("arraylist中的元素个数为:" + size);//3.判断arrayList中是否含有指定元素boolean isExist = list.contains("abc");System.out.println("arraylist中是否含有abc:" + isExist);//4.返回arraylist中元素第一次出现的位置,若没有则返回-1int index = list.indexOf("abc");System.out.println("arraylist中第一次出现abc的位置是" + index);//5.返回arraylist中元素最后一次出现的位置,若没有则返回-1int lastIndex = list.lastIndexOf("abc");System.out.println("arraylist最后一次出现abc的位置是" + lastIndex);//6.返回包含此列表中所有元素的数组(按顺序),相当于数组 API和collection API的桥梁,返回一个object的数组Object[] objectArray = list.toArray();System.out.println("生成的数组为");for(Object obj : objectArray){System.out.println(obj);}//7.返回列表中指定位置的元素,如果超出返回,则会抛出IndexOutOfBoundsExceptionString s = list.get(0);System.out.println("列表中第一个元素是:" + s);//8.用指定的元素替代此列表中指定位置上的元素,返回值是以前位于该位上的元素String old = list.set(0, "abc");System.out.println("第一个元素" + old + "被替换成了abc");//9.将元素插入到列表的尾部System.out.println("是否插入成功:" + list.add("def"));//10.将元素插入到指定位置,返回类型为voidlist.add(0, "ghi");//11.移除指定位置的元素System.out.println("元素" + list.remove(0) + "已被移除");//12.移除此列表中首次出现的指定元素(如果存在)System.out.println("元素是否被移除:" + list.remove("abc"));//13.按照指定 collection 的迭代器所返回的元素顺序,将该 collection 中的所有元素添加到此列表的尾部List<String> list2 = new ArrayList<String>();list2.add(arraylistTest.GenRandomString(5));list2.add(arraylistTest.GenRandomString(5));list.addAll(list2);System.out.println("新列表为:");for(String str: list){System.out.println(str);}//14.移除列表中所有元素list.clear();System.out.println("移除所有元素后列表的长度为:" + list.size());}//生成指定长度的随机字符串public static String GenRandomString(int length){charArray = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();char[] randomChar = new char [length];for(int i = 0; i < randomChar.length; i++){randomChar[i] = charArray[random.nextInt(61)];}return new String(randomChar);}}
- java API中提供的ArrayList的常用方法
- java API中提供的HashMap的常用方法
- java中List,ArrayList,LinkedList的常用方法
- JAVA提供的SOCKET API
- ArrayList的几个常用方法
- 关于ArrayList 的常用方法
- Java中常用的集合类、ArrayList 、HashMap及其遍历方法
- Java.lang.String提供的常用的方法
- java API中常用方法
- ArrayList中常用方法
- Windchill 二次开发中常用的API方法
- [JAVA · 初级]:9.Object类提供的常用方法
- java并发API中ThreadPoolExecutor类提供的执行所有任务和任一任务的2个方法整理
- android api提供的单位换算方法
- JQuery Mobile API提供的实用方法
- Java中常用的字符串API
- java中Math类的常用API
- JAVA中连接HBASE的常用API
- 父子进程间文件描述符的共享
- 关于划分树
- POJ 1905 Expanding Rods
- 腾讯后台开发面试题
- Unicode(UTF-8, UTF-16)令人混淆的概念
- java API中提供的ArrayList的常用方法
- 模版-数学
- 第二章第十六题
- 衡量分类器的性能指标
- SVM概述
- 【黑马程序员】---- Android常用权限整理
- 蓝桥杯(水题)---密码
- iOS 获取IP地址
- Linux Android 反编译