ArrayList
- Hold onto contents using an Array
- Increase 50% size each time
import java.util.*;
public class L
{
public static void main(String args[])
{
ArrayList<Integer> l = new ArrayList<Integer>();
//add
for(int i = 0; i < 10; i++)
l.add(i);
//get, size
for(int i = 0; i < l.size(); i++)
System.out.printf("%4d", l.get(i));
System.out.println();
//set
for(int i = 0; i < l.size(); i++)
if(l.get(i) % 2 == 0)
l.set(i, l.get(i)*10);
System.out.println(l);
}
}
ListIterator
- For List implemented classes
- Increase 50% size each time
import java.util.*;
public class L
{
public static void main(String args[])
{
ArrayList<Integer> l = new ArrayList<Integer>();
//add
for(int i = 0; i < 10; i++)
l.add(i);
System.out.println(l);
ListIterator itr = l.listIterator();
//hasNext, next, set
while(itr.hasNext())
{
int e = (Integer)itr.next();
System.out.printf("%4d: %4d%n", (Integer)itr.nextIndex(), e);
itr.set(e*10);
}
System.out.println(l);
//hasPrevious, previous, remove
while(itr.hasPrevious())
{
int e = (Integer)itr.previous();
if((e/10)%2 == 1)
itr.remove();
}
System.out.println(l);
}
}
LinkedList
import java.util.*;
public class L
{
public static void main(String args[])
{
LinkedList<Integer> l = new LinkedList<Integer>();
//add
for(int i = 0; i < 10; i++)
l.add(i);
//addFirst, addLast
l.addFirst(100);
l.addLast(200);
System.out.println(l);
//get, size
for(int i = 0; i < l.size(); i++)
System.out.printf("%4d", l.get(i));
System.out.println();
//set
for(int i = 0; i < l.size(); i++)
if(l.get(i) % 2 == 0)
l.set(i, l.get(i)*10);
System.out.println(l);
//contains
if(l.contains(3))
System.out.println("Contine 3 ...");
//remove
l.remove(5);
System.out.println(l);
}
}
Vector
import java.util.*;
public class L
{
public static void main(String args[])
{
LinkedList<Integer> l = new LinkedList<Integer>();
for(int i = 0; i < 10; i++)
l.add(i);
Vector<Integer> v = new Vector<Integer>(l);
v.add(1, 100);
for(int i = 0; i < v.size(); i++)
System.out.printf("%4d", v.get(i));
System.out.println();
for(int i = 0; i < v.size(); i++)
{
if(v.get(i)%2 == 0)
v.set(i, v.get(i)*10);
}
System.out.println(v);
}
}
Reference