1.编写程序测试Java集合框架中各种常用类的 基本操作(包括添加元素,删除元素,查找元素,遍历集合元素等)
(1)常用集合类-ArrayList类(内部实现基于数组)
将整型对象元素顺序加入ArrayList中
package yztext07;
import java.util.*;
public class Test {
public static void main(String[] args) {
List al = new ArrayList();
al.add(new Integer(1));
al.add(new Integer(4));
al.add(new Integer(2));
for (int i = 0; i < 3; i++) {
Integer m = (Integer) al.get(i);
System.out.println(m);
}
}
}
运行结果:9
8
7
6
5
4
3
2
1
0
(2)常用集合类-linkedList类 (内部实现基于链表)
import java.util.*;
public class IntObjectlink{
public static void main(String args[]) {
Integer tem;
try{
List lst = new linkedList();
for (int i=0; i<10 ; i++ ){
lst.addFirst(new Integer(i));
}
for (int i=0; i<10; i++ ){
tem = lst.removeFirst();
System.out.println(tem.intValue());
}
}
catch(Exception e){
System.out.println(“error");
}
}
}
(3)常用集合类-ArrayDeque (提供栈结构实现)
import java.util.*;
public class Stacks{
public static void main(String[] args){
Deque stk = new ArrayDeque();
for(int i=0;i<10;i++)
stk.push(i); //addFirst()
System.out.println("stk="+ stk);
System.out.println("popping elements:");
while(stk.size()!=0)
//removeFirst()
System.out.println(stk.pop());
}
}
运行结果:
stk=[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
popping elements:
9
8
7
6
5
4
3
2
1
0



