编写程序测试Java集合框架中各种常用类的基本操作(包括添加元素,删除元素,查找元素,遍历集合元素等)

Vector:
从AbstractList派生而来,可自动增加容量来容纳所需对象,实现List接口,元素之间有序。

import java.util.*;
public class vector {
	public static void main(String[] args) {
		Vector<Integer> v=new Vector();
		for(int i=1;i<=10;i++)//添加元素
			v.addElement(new Integer(i));
		for(Iterator it=v.iterator();it.hasNext();) {//遍历元素
			Integer i=(Integer) it.next();
			System.out.print(i+" ");
		}
		System.out.println();
		v.remove(3);//删除元素
		for(Iterator it=v.iterator();it.hasNext();) {
			Integer i=(Integer) it.next();
			System.out.print(i+" ");
		}
		System.out.println();
	}
}
1 2 3 4 5 6 7 8 9 10
1 2 3 5 6 7 8 9 10

Stack:
从Vector派生出,增加了栈的实现方法。

import java.util.*;
public class stack {
	public static void main(String[] args) {
		Stack<Integer> s=new Stack();
		for(int i=0;i<10;i++)//入栈
			s.push(new Integer(i));
		s.add(new Integer(10));//添加
		for(Iterator it=s.iterator();it.hasNext();) {//遍历
			System.out.print((Integer)it.next()+" ");
		}
		System.out.println();
		s.remove(3);//删除
		for(Iterator it=s.iterator();it.hasNext();) {
			System.out.print((Integer)it.next()+" ");
		}
		System.out.println();
		s.pop();//出栈
		for(Iterator it=s.iterator();it.hasNext();) {
			System.out.print((Integer)it.next()+" ");
		}
		System.out.println();
		while(!s.empty()) {
			System.out.print(s.pop()+" ");
		}
	}
}
0 1 2 3 4 5 6 7 8 9 10
0 1 2 4 5 6 7 8 9 10
0 1 2 4 5 6 7 8 9
9 8 7 6 5 4 2 1 0

LinkedList:
从AbstractList派生而来,实现了一个链表,由这个类定义的链表也可以像栈和队列一样被使用。

package chapter10;

import java.util.*;
public class linkedlist {
	public static void main(String[] args) {
		LinkedList<Integer> l=new LinkedList();
		for(int i=0;i<5;i++)//从头添加
			l.addFirst(new Integer(i));
		for(int i=5;i<10;i++)//从尾添加
			l.addLast(new Integer(i));
		for(Iterator it=l.iterator();it.hasNext();) {//遍历
			System.out.print((Integer)it.next()+" ");
		}
		System.out.println();
		l.removeFirst();//从头删
		l.removeLast();//从尾删
		for(Iterator it=l.iterator();it.hasNext();) {//遍历
			System.out.print((Integer)it.next()+" ");
		}
		System.out.println();
	}
}

4 3 2 1 0 5 6 7 8 9
3 2 1 0 5 6 7 8

ArrayList:
由AbstractList派生而来,规模可变,且能像链表一样被访问。

import java.util.*;

public class arraylist {
	public static void main(String[] args) {
		ArrayList<Integer> al=new ArrayList();
		for(int i=1;i<=10;i++)//增加元素
			al.add(new Integer(i));
		for(Iterator it=al.iterator();it.hasNext();)//遍历元素
			System.out.print((Integer)it.next()+" ");
		System.out.println();
		al.remove(4);//删除元素
		for(Iterator it=al.iterator();it.hasNext();)
			System.out.print((Integer)it.next()+" ");
	}
}

1 2 3 4 5 6 7 8 9 10
1 2 3 4 6 7 8 9 10

Collections:

集合工具类,方便对集合的操作。这个类不需要创建对象,内部提供的都是静态方法。

Collections.sort(list);//list集合进行元素的自然顺序排序。
Collections.sort(list,new ComparatorByLen());//按指定的比较器方法排序。
class ComparatorByLen implements Comparator<String>{
  public int compare(String s1,String s2){
     int temp = s1.length()-s2.length();
     return temp==0?s1.compareTo(s2):temp;
  }
}
Collections.max(list);//返回list中字典顺序最大的元素。
int index = Collections.binarySearch(list,"zz");//二分查找,返回角标。
Collections.reverseOrder();//逆向反转排序。
Collections.shuffle(list);//随机对list中的元素进行位置的置换。

//将非同步集合转成同步集合的方法:Collections中的  XXX synchronizedXXX(XXX);  
//原理:定义一个类,将集合所有的方法加同一把锁后返回。
List synchronizedList(list);
Map synchronizedMap(map);


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值