好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java中的ArrayList类常用方法和遍历

ArrayList类常用方法和遍历

 ArrayList类对于元素的操作,基本体现在——增、删、查。

常用的方法有

public boolean add(E e) :将指定的元素添加到此集合的尾部。 public E remove(int index) :移除此集合中指定位置上的元素。返回被删除的元素。 public E get(int index) :返回此集合中指定位置上的元素。返回获取的元素。 public int size() :返回此集合中的元素数。遍历集合时,可以控制索引范围,防止越界。 contains(object obj) :判断是否含有指定元素 public E set(int index, String element) :把此集合中指定索引的元素,改为新的元素

这些都是最基本的方法,操作非常简单,代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

public class ArrayListDemo {

     public static void main(String[] args) {

         //创建集合对象

         ArrayList<String> list = new ArrayList<String>();

         //添加元素

         list.add( "hello" );

         list.add( "world" );

         list.add( "java" );

         //public E get(int index):返回指定索引处的元素

         System.out.println( "get:" +list.get( 0 ));

         System.out.println( "get:" +list.get( 1 ));

         System.out.println( "get:" +list.get( 2 ));

         //public int size():返回集合中的元素的个数

         System.out.println( "size:" +list.size());

         //public E remove(int index):删除指定索引处的元素,返回被删除的元素

         System.out.println( "remove:" +list.remove( 0 ));

         //遍历输出

         for ( int i = 0 ; i < list.size(); i++){

             System.out.println(list.get(i));

         }

     }

}

ArrayList类方法总结

关于ArrayList

ArrayList是集合框架List接口的实现类(数组实现)

List接口是一个有序的 Collection,使用此接口能够精确的控制每个元素插入的位置,能够通过索引(元素在List中位置,类似于数组的下标)来访问List中的元素,第一个元素的索引为 0,而且允许有相同的元素。List 接口存储一组不唯一,有序(插入顺序)的对象。

ArrayList实现了List的接口,实现了可变大小的数组,随机访问和遍历元素时,提供更好的性能。该类也是非同步的,在多线程的情况下不要使用。ArrayList 增长当前长度的50%,插入删除效率低。

常用方法总结

构建ArrayList

1.不初始化起容量

?

1

ArrayList al = new ArrayList(); //默认容量为0,当数组容量满时数组会自动一当前数组容量的2倍扩容

2.初始化容量

?

1

ArrayList al = new ArrayList( 3 ); //初始容量为3

3.以一个集合或数组初始化

?

1

ArrayList al = new ArrayList(a); //a为集合或数组

添加元素

?

1

2

3

4

5

//1.ArrayList名.add(object value)

ArrayList al = new ArrayList();

al.add( "a" );

//2.将元素插入到索引处(不过其有一定的限制性,必须在数组长度以内插入数组)

al.insert( int index,object value);

删除元素

?

1

2

3

al.Remove(object obj); //移除数组中的obj元素

al.RemoveAt( int index); //移除索引为index的数字元素

al.RemoveRange( int indext, int count); //移除从索引index开始,移除count个元素

查找元素

查找元素有Contains()、IndexOf()、LastIndexOf()3中方法

?

1

2

//boolean contains(Object o)

al.Contains(object obj); //查找数组中是否有obj元素,返回类型为boolean存在返回true;

IndexOf()有两个重载方法 起用法如下:

?

1

2

3

4

//int indexOf(Object o)

al.IndexOf(object obj); //从0开始查找obj元素,只第一个obj元素,并返回起在数组中的位置,如果不存在,返回-1;

al.IndexOf(object obj, int startIndex); //从startIndex开始查找obj元素,只第一个obj元素,并返回起在数组中的位置,

al.IndexOf(object obj, int startIndex, int count); //从startIndex开始想后查找count个元素,如果存在obj元素,则返回其在数组中的位置

al.LastIndexOf()方法与IndexOf()用法相同,它也有两个重载,其不同的是,LastIndexOf(obj)是查找要obj最后出现的位置

获取元素

?

1

al.get(index);

获取ArrayList数组长度

?

1

al.size();

检查是否为空

?

1

2

//boolean isEmpty()

al.isEmpty();

遍历ArrayList

1.获取数组长度,循环遍历

?

1

2

for ( int i = 0 , i < al.size(); i++){

}

2.使用for-each循环

?

1

2

for (object e : al){

}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://HdhCmsTestcnblogs测试数据/libinhong/p/10988935.html

查看更多关于Java中的ArrayList类常用方法和遍历的详细内容...

  阅读:16次