9i Array List, Vector and Generics (1)
9i Array List, Vector and Generics (1)
• ArrayList
• Vectors
• Generics
Array List
• The ArrayList class is a resizable array, which can be
found in the java.util package.
• The difference between a built-in array and an ArrayList
in Java, is that the size of an array cannot be modified.
• While elements can be added and removed from an
ArrayList whenever you want.
• The syntax is also slightly different.
Create an ArrayList object called cars that will store strings:
import java.util.ArrayList; // import the ArrayList class
cars.add("Volvo"); }
cars.add("BMW");
Sort an array list of integers
• Method 3:
Vector object= new vector(int initialcapacity, capacityIncrement)
Vector vec= new Vector(4, 6)
Here we have provided two arguments. The initial capacity is 4 and
capacityIncrement is 6. It means upon insertion of 5th element the
size would be 10 (4+6) and on 11th insertion it would be 16(10+6).
Commonly used methods()
void
addElement(Object It inserts the element at the end of
element) the Vector.
T value; }
public T getValue(){ public class GenericDemo {
return value; public static void main(String[]
args){
}
Container<Number> obj = new
public void setValue(T value){ Container<>();
this.value=value; obj.value=9;
} obj.Show();
public void Show() { obj.demo(new
System.out.println(value); Arraylist<Integer>());
} } Container: Integer
} ArrayList: Number
THANK YOU….
25