ArrayList Class Example 1
Import java.util.ArrayList;
public class ArrayListDemo1 {
public static void Main (string[] args) {
arraylist<string> list = new arraylist<string> ();//Create an empty ArrayList object (generic)
List.add ("R"); adding elements
List.add ("s");
List.add ("w");
List.add ("C");
List.add ("V");
Get array elements
for (String a:list) {
System.out.println (a);
}
Determine if a value exists
String t = new String ("W");
int g = List.indexof (t);
for (int i=0;i<list.size (); i++) {
System.out.println (g);
}
if (g<0) {
System.out.println ("W traversal not");
}else{
SYSTEM.OUT.PRINTLN (t+ "index, bit in" +g);
}
List.remove (3); Delete an element at an index location
System.out.println ("delete element at index position after 3");
for (int i=0;i<list.size (); i++) {
System.out.println (String) (List.get (i)));
}
}
}
Printing results:
R
S
W
C
V
2
2
2
2
2
W index, bit at 2
Delete the element at index position after 3
R
S
W
V
This article is from the Basic Java Essentials Note blog, so be sure to keep this source http://695412.blog.51cto.com/10289099/1660561
A collection of Java