Java常用类实战


ArrayList类

题:假设52张扑克牌(去掉大小王),实现随机洗牌操作,为参加游戏的人每人生成一手牌,每手牌的牌数是指定的,并将每个人分到的牌按花色排序后输出。

import java.util.*;

public class TestDealCard {
	public static void main(String[] args) {
		int numHands=4;
		int cardsPerHand=12;
		String[] suit= {"♠","♣","♥","♦"};
		String[] rank= {"A","1","2","3","4","5","6","7","8","9","10","J","Q","K"};
		List deck = new ArrayList();
		for(int i=0;i<suit.length;i++) {
			for(int j=0;j<rank.length;j++) {
				deck.add(suit[i]+rank[j]);
			}
		}
		
		Collections.shuffle(deck);
		
		for(int i=0;i<numHands;i++) {
			List p=dealCard(deck, cardsPerHand);
			Collections.sort(p);
			System.out.println(deck);
		}
	}
	
	public static List dealCard(List deck, int n) {
		int deckSize=deck.size();
		List handView=deck.subList(deckSize-n, deckSize);
		List hand=new ArrayList(handView);
		handView.clear();
		return hand;
	}
}

LinkList类

题:用LinkList写一个Stack的数据结构测试。

import java.util.*;

public class TestStack {
	//创建链表linkList
	LinkedList linkList=new LinkedList<Object>();
	
	public void push(Object obj) {
		linkList.addLast(obj);
	}
	
	public void clear() {
		linkList.clear();
	}
	
	public boolean isEmpty() {
		return linkList.isEmpty();
	}
	
	public int getSize() {
		return linkList.size();
	}
	
	public Object pop() {
		if(!linkList.isEmpty())
			return linkList.removeLast();
		return "栈内无数";
	}
	
	public static void main(String[] args) {
		TestStack myStack=new TestStack();
		myStack.push(3);
		myStack.push(4);
		myStack.push(5);
		System.out.println(myStack.pop());
		System.out.println(myStack.pop());
	}
}

Vector类

题:写一个简单的Vector测试。

import java.util.*;

public class TestVector {
	public static void main(String[] args) {
		Vector v=new Vector(4);
		v.add("J");
		v.add("A");
		v.add("J");
		v.add("E");
		v.add("L");
		v.remove("J");
		v.remove(0);
		
		int size=v.size();
		System.out.println("Size:"+size);
		
		for(int i=0;i<size;i++) {
			System.out.println(v.get(i));
		}
	}
}

题:演示一个简单的学生信息管理:利用向量记录实现学生管理,能支持学生对象的增加、删除操作,每个学生对象包括学号,性别,姓名。删除学生必须输入学生的学号。可以设计一个操作菜单,包括增加,删除,显示以及退出4项。

import java.util.*;
import java.io.*;

public class Student {
	String name;
	long stno;
	String sex;
	
	public Student(String name,long stno,String sex) {
		this.name=name;
		this.stno=stno;
		this.sex=sex;
	}

	public String toString() {
		return "姓名:"+name+",学号:"+stno+",性别:"+sex;
	}
	
	public static void main(String[] args) {
		Vector<Student> group=new Vector<Student>();
		outer:
		while(true) {
			String ch=input("选择:1--增加,2--删除,3--显示,4--退出");
			int choice=Integer.parseInt(ch);
			switch(choice) {
				case 1:
					group.add(inputStudent());
					break;
				case 2:
					long stno=Long.parseLong(input("请输入学号:"));
					for(int k=0;k<group.size();k++) {
						Student x=(Student)group.get(k);
						if(x.stno==stno)
							group.removeElement(x);
					}
					break;
				case 3:
					//迭代输出Vector存储
					Iterator p=group.iterator();
					while(p.hasNext()){
						System.out.println("==>"+p.next());
					}
					break;
				case 4:
					break outer;
			}
		}
	}
	
	public static String input(String hint) {
		String x=null;
		try {
			//字符串输入
			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
			System.out.println(hint);
			x=br.readLine();
		}
		catch(IOException e) {}
		return x;
	}
	
	public static Student inputStudent() {
		String name=input("请输入姓名:");
		long stno=Long.parseLong(input("请输入学号:"));
		String sex=input("请输入性别:");
		return new Student(name,stno,sex);
	}
}

Map类

题:写一个测试映射集合的代码。

import java.util.*;

public class TestTreeMap {
	public static void main(String[] args) {
		Map map=new HashMap();
		map.put("c","ccc");
		map.put("a","aaa");
		map.put("b","bbb");
		map.put("d","ddd");
		Iterator iter=map.keySet().iterator();
		while(iter.hasNext()) {
			Object key=iter.next();
			System.out.println("tab key"+key.toString()+"value="+map.get(key));
		}
		TreeMap tab=new TreeMap();
		tab.put("c","ccc");
		tab.put("a","aaa");
		tab.put("b","bbb");
		tab.put("d","ddd");
		Iterator iter2=tab.keySet().iterator();
		while(iter2.hasNext()) {
			Object key=iter2.next();
			System.out.println("map key"+key.toString()+"value="+tab.get(key));
		}
	}
	
}

文章作者: Peyton
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Peyton !
  目录