Java 集合框架Example-1

课程添加查询
public class Course {
	public String id;
	public String name;
	public Course(String id,String name){
		this.id=id;
		this.name=name;
	}


import java.util.HashSet;
import java.util.Set;
/*
 * 学生类
 */
public class Student {
	public String id;
	public String name;
	public Set courses;
	public Student(String id,String name){
		this.id=id;
		this.name=name;
		this.courses=new HashSet();
	}

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import javax.swing.text.InternationalFormatter;
/*
 * 备选课程类
 */
public class ListTest {
	/*
	 * 用于存放备选课程的List
	 */
	public List coursesToSelect;
	public ListTest(){
		this.coursesToSelect= new ArrayList();
	}
/*
 * 用于往coursesToSelect中添加备选课程
 */
	public void testAdd(){
		/*
		 * 创建一个课程对象,并通过调用add,添加list中
		 */
		Course cr1=new Course("1"," 数据结构");
		coursesToSelect.add(cr1);	
		Course temp=(Course) coursesToSelect.get(0);
		System.out.println("添加了:"+temp.id+temp.name);
		
		Course cr2= new Course("2"," C language");
		coursesToSelect.add(0,cr2);
		Course temp2=(Course) coursesToSelect.get(0);
		System.out.println("添加了:"+ temp2.id+temp2.name);
		
		coursesToSelect.add(cr1);	
		Course temp0=(Course) coursesToSelect.get(0);
		System.out.println("添加了:"+temp0.id+temp0.name);
		
		Course[] course={new Course("3"," 离散数学"),new Course("4"," 汇编语言")};
		coursesToSelect.addAll(Arrays.asList(course));
		Course temp3=(Course) coursesToSelect.get(3);
		Course temp4=(Course) coursesToSelect.get(4);
		System.out.println("添加了:" + temp3.id+temp3.name+";"+temp4.id+temp4.name);
		
		Course[] course2={new Course("5"," 高等数学"),new Course("6"," 大学英语")};
		coursesToSelect.addAll(2,Arrays.asList(course2));
		Course temp5=(Course) coursesToSelect.get(2);
		Course temp6=(Course) coursesToSelect.get(3);
		System.out.println("添加了:" + temp5.id+temp5.name+";"+temp6.id+temp6.name);
	
	}
	/*
	 * 取得List中的元素的方法
	 */
	public void testGet(){
		int size=coursesToSelect.size();
		System.out.println("有如下课程: ");
		for(int i=0;i<size;i++){
			Course cr=(Course) coursesToSelect.get(i);
			System.out.println("课程: "+cr.id+cr.name);
		}
	}
	/*
	 * 通过迭代器来遍历List
	 */
	public void testIterator(){
		//通过集合的iterator方法,取得迭代器的实例
		Iterator it=coursesToSelect.iterator();
		while(it.hasNext()){
			Course cr=(Course) it.next();
			System.out.println("课程: "+cr.id+":"+cr.name);
		}
	}
	
	/*
	 * For each
	 */
	
	public void testForEach(){
		for (Object obj:coursesToSelect){
			Course cr=(Course) obj;
			System.out.println("课程: "+cr.id+":"+cr.name);
		}
	}
	
	public static void main(String[] args){
		ListTest Lt=new  ListTest();
		Lt.testAdd();
		Lt.testGet();
		Lt.testIterator();
		Lt.testForEach();
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值