ArrayList variable array :
ArrayList inherits the Abstractlist abstract class, implements the list interface, and the underlying is based on the array implementation. Can hold null, except for non-synchronous, roughly the same vector. For fast access, replication, serialization.
constructor function:
ArrayList () Default initial capacity is 10
ArrayList (int initialcapacity) Specify initial capacity
Expand capacity before adding large amounts of data.
void ensurecapacity (int mincapacity)
The size () method is now the actual size of the array, not the size of the array capacity.
ArrayList thread is out of sync, List list=collections.synchronizedlist (New ArrayList (...)); Implement synchronization.
Common methods:
add element to the tail of the Boolean add (element e)
void Add (int index,element e) Specifies the location to add
Boolean addall (int index,collection<? extends e> c) Specify the location to add all elements of the specified collection
Boolean AddAll (collection<? extends e> c) adds all elements of the specified collection at the tail
void clear () empty linked list
void ensurecapacity (int mincapacity) increases the list capacity
Object clone () shallow copy ArrayList instance
Remove (int index) deletes the specified position element
protected void RemoveRange (int start,int end) Deletes the element of the specified range
Get (int index) takes the specified position element
Object[] ToArray () linked list conversions to arrays
<T> t[] ToArray (t[] a) linked list converted to an array
E set (int index,e element) specifies position substitution
Contains (Object o) Whether the search contains the specified element
int indexOf (Object o) returns the position where the element first appears in the list, no then returns 1
int lastIndexOf (Object o) Last occurrence position, IBID.
Boolean IsEmpty () determines whether the linked list is empty
int size () returns the chain table length
Example:
Package test;
import java.util.*;
Public class Arraylisttest {
Public Static void Main (String args[]) {
arraylist<string> list = new arraylist<string> ();
linkedlist<string> Listlink = new linkedlist<string> ();
Listlink.add ("ListLink1");
Listlink.add ("ListLink2");
Listlink.add ("ListLink3");
Listlink.add ("ListLink4");
Listlink.add ("ListLink5");
List.add ("List1");
List.add ("List2");
List.add (2, "list3");
List.add ("List4");
List.addall (Listlink);
System. out. println ("1-the ArrayList contains the following elements:" + list);
int pos = list.indexof ("List2");//Returns the position of the first occurrence of the element in the list
System. out. println ("2-the Index of List2 is:" + pos);
boolean emptyfg = List.isEmpty ();
System. out. println ("3-the arraylist are empty or not:" + EMPTYFG);
int size = list.size ();
System. out. println ("4-the size of the list is:" + size);
Boolean element = List.contains ("List5");//Determine whether the element exists or not
System. out. println ("5-the ArrayList contains the object list5:" + Element);
String item = list.get (0);//Gets the specified position element
System. out. println ("6-the item was the index 0 is:" + Item);
System. out. println ("7-for cycle linked list");
for (int i = 0; i < list.size (); i++) {
System. out. println ("8-index:" + i + "-Item:" + list.get (i));
}
System. out. println ("9-foreach cycle linked list");
for (String str:list) {
System. out. println ("10-element:" + str);
}
System. out. println ("11-iterator loop linked list using iterators");
for (Iterator<string> it = List.iterator (); It.hasnext ();) {
System. out. println ("12-element is:" + it.next ());
}
List.set (1, "position 1set element");
System. out. println ("13-replaces the specified position element:" + list);
List.remove (0);
List.remove ("List3");
System. out. println ("14-After deleting elements:" + list);
string[] Simplearray = List.toarray (new string[list.size ());
System. out. println ("15-list conversions to arrays:" + Arrays. ) ToString(Simplearray));//requires tostring method conversion, otherwise the output is an address
}
}
Java collection ArrayList linked list basics