Stringbuffer类
{
StringBuffer sb1 = new StringBuffer("hello");
StringBuffer sb2 = new StringBuffer("world");
change(sb1,sb2);
System.out.println(sb1 + "---------" + sb2);
}
public static void change(StringBuffer s1, StringBuffer s2){
s1 = s2;
System.out.println(s1);
s2.append(s1);
}
输出结果为
world
hello---------worldworld
ArrayList类
public class Main{
public static void main(String[] args) {
ArrayList a1 = new ArrayList();
a1.add("C");
a1.add("A");
a1.add("E");
a1.add("B");
a1.add("D");
a1.add("F");
System.out.println("the context a1 is " + a1);
a1.add(2,"A1");
System.out.println("the context a1 is " + a1);
a1.remove("F");
System.out.println("the context a1 is " + a1);
a1.remove(3);
System.out.println("the context a1 is " + a1);
System.out.println("the num of a1 is " + a1.size());
}
}
运行结果
the context a1 is [C, A, E, B, D, F]
the context a1 is [C, A, A1, E, B, D, F]
the context a1 is [C, A, A1, E, B, D]
the context a1 is [C, A, A1, B, D]
the num of a1 is 5
LinkedList类
public class Main{
public static void main(String[] args) {
LinkedList a1 = new LinkedList();
a1.add("C");
a1.add("A");
a1.add("E");
a1.add("B");
a1.add("D");
a1.add("F");
System.out.println("the context a1 is " + a1);
a1.add(2,"A1");
System.out.println("the context a1 is " + a1);
a1.addFirst("Z"); //在链表第一个位置插入
System.out.println("the context a1 is " + a1);
a1.addLast("M"); //在链表最后一个位置插入
System.out.println("the context a1 is " + a1);
a1.remove("F");
System.out.println("the context a1 is " + a1);
a1.remove(3);
System.out.println("the context a1 is " + a1);
Object val = a1.get(2);
a1.set(2,(String)val + "hello"); //(Integer)val不对
System.out.println("the context a1 is " + a1);
System.out.println("the num of a1 is " + a1.size());
}
}
运行结果
the context a1 is [C, A, E, B, D, F]
the context a1 is [C, A, A1, E, B, D, F]
the context a1 is [Z, C, A, A1, E, B, D, F]
the context a1 is [Z, C, A, A1, E, B, D, F, M]
the context a1 is [Z, C, A, A1, E, B, D, M]
the context a1 is [Z, C, A, E, B, D, M]
the context a1 is [Z, C, Ahello, E, B, D, M]
the num of a1 is 7
HashSet类
插入的元素不能重复,不能保证元素的存取顺序一致,没有带索引的方法,所以不能通过普通for循环进行遍历,散列法的优点是即使对于大的集合,它的一些基本操作,如add(),remove(),size()和contains()等方法的运行时间保持不变
public class Main{
public static void main(String[] args) {
HashSet a1 = new HashSet();
a1.add("F");
a1.add("A");
a1.add("C");
a1.add("B");
a1.add("D");
a1.add("C"); //插入相同的元素,返回false
System.out.println("the context a1 is " + a1);
System.out.println("the num of a1 is " + a1.size());
}
}
运行结果
the context a1 is [A, B, C, D, F]
the num of a1 is 5
TreeSet类
能够自动排序