JAVA collection box
JAVA 's collection framework is a series of tools provided in the java.util package, which provides a standard way for programs to process object groups
1. Collection implementation class ArrayList
package TestArrayList;
import java.util.ArrayList;
public class TestArrayList
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
ArrayList<String> all = new ArrayList<String>();
// Add element method
all.add("a");
all.add("b");
all.add("c");
all.add("d");
System. out .println( " The length of all is: " + all .size());
. System OUT .println ( "All content is: " + All ); // rewrite the object of toString function
System. out .println( " Set the first element to first" );
all.add(0, "first");
all.add("e");
System. out .println( " The length of all is: " + all .size());
. System OUT .println ( "All content is: " + All ); // rewrite the object of toString function
System. out .println( " Set the b element to B" );
all.set(2,"B");
. System OUT .println ( "All content is: " + All ); // rewrite the object of toString function
// Delete method
System. out .println( " Delete d element " );
all.remove("d");
System. out .println( " The content of all is: " + all );
all .remove(4); // Remove the element at the specified position
System. out .println( " The content of all is: " + all );
// Query method
for(String s:all)
{
System.out.println(s+" ");
}
for(int i=0;i<all.size();i++)
{
System.out.println(all.get(i)+" ");
}
// Interaction between ArrayList and array
System. out .println( " Print s1 array " );
String[] s1 = new String[all.size()];
s1 = all.toArray(s1);
for(String s:s1)
{
System.out.println(s+" ");
}
// Pass value when constructing ArrayList
ArrayList<String> all2 = new ArrayList<String>(all);
System.out.println(all2);
System.out.println(all.equals(all2));//比较是否相等
}
}