java ArrayList详解及其方法

Java中的ArrayList

  • ArrayList是一个由Collection框架中的List接口实现的类。

  • ArrayList实现List接口,而List接口是Collection接口的子接口,因此最终ArrayList可以包含List和Collection接口的方法。

1)Collection 接口方法:

  1. boolean add(Object o)

  2. boolean addAll(Collection c)

  3. boolean remove(Object o)

  4. boolean removeAll(Collection c)

  5. boolean retainAll(Collection c)

  6. void clear()

  7. boolean isEmpty()

  8. int size()

  9. boolean contains(Object o)

  10. boolean containsAll(Collection c)

  11. Object[] toArray()

  12. Iterator iterator()

例:

我们将从下面给出的示例中了解Collection接口方法的工作方式:

import java.util.*;

class CollectionMethods{
	public static void main(String[] args){
		ArrayList al = new ArrayList();

		System.out.println("boolean add(Object o) : Add an object in ArrayList");
		al.add(10);
		al.add(20);
		al.add(30);
		al.add(40);
		al.add(50);
		System.out.println("Updated ArrayList is : " + al);

		System.out.println("boolean addAll(Collection c) : Add Collection in ArrayList");
		al.addAll(al);
		System.out.println("Updated ArrayList is : " + al);

		System.out.println("boolean contains(Object o) : it returns true if element contain");
		al.contains(10);
		System.out.println("Updated ArrayList is : " + al);

		System.out.println("boolean containsAll(Collection c) : it returns true if collection contain");
		al.containsAll(al);
		System.out.println("Updated ArrayList is : " + al);

		System.out.println("boolean toArray() : collection object convert into array");
		Object[] values = al.toArray();
		for(int i=0;i<values.length;++i)
		System.out.println("Display in Array form  : " + values[i]);

		System.out.println("boolean iterator() : it iterates collection object");
		Iterator it = al.iterator();
		
		while(it.hasNext())
			System.out.println("Iterating ArrayList : " + it.next());
	}
}

输出结果

D:\Java Articles>java CollectionMethods
boolean add(Object o) : Add an object in ArrayList
Updated ArrayList is : [10, 20, 30, 40, 50]
boolean addAll(Collection c) : Add Collection in ArrayList
Updated ArrayList is : [10, 20, 30, 40, 50, 10, 20, 30, 40, 50]
boolean contains(Object o) : it returns true if element contain
Updated ArrayList is : [10, 20, 30, 40, 50, 10, 20, 30, 40, 50]
boolean containsAll(Collection c) : it returns true if collection contain
Updated ArrayList is : [10, 20, 30, 40, 50, 10, 20, 30, 40, 50]
boolean toArray() : collection object convert into array
Display in Array form  : 10
Display in Array form  : 20
Display in Array form  : 30
Display in Array form  : 40
Display in Array form  : 50
Display in Array form  : 10
Display in Array form  : 20
Display in Array form  : 30
Display in Array form  : 40
Display in Array form  : 50
boolean iterator() : it iterates collection object
Iterating ArrayList : 10
Iterating ArrayList : 20
Iterating ArrayList : 30
Iterating ArrayList : 40
Iterating ArrayList : 50
Iterating ArrayList : 10
Iterating ArrayList : 20
Iterating ArrayList : 30
Iterating ArrayList : 40
Iterating ArrayList : 50

2)列表接口方法

  1. boolean add(int index,Object o)

  2. boolean addAll(int index,Collection c)

  3. Object remove(int index)

  4. Object get(int index)

  5. Object set(int index,Object new)

  6. int indexOf(Object o)

  7. int lastIndexOf(Object o)

  8. ListIterator listiterator()

示例

我们将从以下示例中了解List接口方法的工作方式:

import java.util.*;

class ListMethods{
	public static void main(String[] args){
		ArrayList al = new ArrayList();
		LinkedList ll = new LinkedList();

		ll.add(10);
		ll.add(20);
		ll.add(30);
		ll.add(40);
		ll.add(50);

		al.add(10);
		al.add(20);
		al.add(30);
		al.add(40);
		al.add(50);
		System.out.println("Basic arraylist are :"+al);

		System.out.println(" add(int index,Object o): It adds an object to a specified index" );
		al.add(2,25);
		System.out.println("Updated arraylist are :"+al);

		System.out.println(" addAll(int index,Collection c): It adds collection to a specified index" );
		al.addAll(6,al);
		System.out.println("Updated arraylist are :"+al);

		System.out.println(" get(int index): It returns object from a particular index" );
		int value = (int)al.get(2);
		System.out.println("Return value of  :"+value);

		System.out.println(" indexOf(Object o): It returns an index of a particular object" );
		int index = (int)al.indexOf(25);
		System.out.println("Return index of a particular object :"+index);

		System.out.println(" lastIndexOf(Object o): It returns a last occurrence index of a particular object" );
		int last_index = (int)al.lastIndexOf(50);
		System.out.println("Return index of a particular object :"+last_index);

		System.out.println(" listIterator(): It iterates list object" );
		ListIterator li = ll.listIterator();
		while(li.hasNext())
			System.out.println("Display objects in list form :"+li.next());
	}
}

输出结果

D:\Java Articles>java ListMethods
Basic arraylist are :[10, 20, 30, 40, 50]
add(int index,Object o): It adds an object to a specified index
Updated arraylist are :[10, 20, 25, 30, 40, 50]
addAll(int index,Collection c): It adds collection to a specified index
Updated arraylist are :[10, 20, 25, 30, 40, 50, 10, 20, 25, 30, 40, 50]
get(int index): It returns object from a particular index
Return value of  :25
indexOf(Object o): It returns an index of a particular object
Return index of a particular object :2
lastIndexOf(Object o): It returns a last occurrence index of a particular object
Return index of a particular object :11
listIterator(): It iterates list object
Display objects in list form :10
Display objects in list form :20
Display objects in list form :30
Display objects in list form :40
Display objects in list form :50