码迷,mamicode.com
首页 > 其他好文 > 详细

mooc_学生所选课程

时间:2015-11-12 00:01:25      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

用一个集合Set存放学生所选课程
接口不能实例化,所以用其实现类进行实例化

set接口不能实例化,通过HashSet这个实现类来进行实例化,需要导入包
this.courses=new HashSet();

 

list接口 ArrayList实现类

this.courseToSelect=new ArrayList(); //才能保证容器能被实例化。

 

ArrayList.get(int index) 一定要注意,a使用dd()或者是addAll()方法插入的元素一律会变成Object类型,那么使用get()方法取出时也将是Object类型的,这是接受取出的元素时,要做相应的类型强转,具体见地理程序。
ArrayList.add(int index)在指定位置插入一个元素,这个指定的位置一定要小于当前序列表的长度,当等于其长度时,则在该数组表的末尾插入
ArrayList.addAll(int index, List L) 在指定位置插入一个序列表的

ArrayList.addAll(List L) 在已有ArrayList的末尾插入一个list序列

把数组转换为ArrayList:
1.Element[] a = {new 对象1,new 对象2....}//创建
2.main 方法所在类.addAll(Array.asList(a));//添加
3.每个数组元素依次转换为指定对象类型//转换

 

List类的addAll removeAll是集合的操作,使用Arrays.asList(array)方法

 

向List中添加元素的几种方法:
新建List类
public List courseToSelect;
初始化List类
public ListTest()
{
this.courseToSelect=new ArrayList();
}
向List中添加数据
method_1
courseToSelect.add(对象1);
method_2
courseToSelect.add(位置,对象2);
method_3
courseToSelect.addAll(Arrays.asList(数组_对象3));
method_4
courseToSelect.addAll(位置,Arrays.asList(数组_对象4));
获取
Course temp=(Course)courseToSelect.get(位置);

 

addAll 和 removeAll都是对集合操作,因此都需要把数组转化成集合。

 

 

课程查询

/*
* 用迭代器遍历list
* @param args
*/
public void testIterator()
{
Iterator it=courseToSelect.iterator();
while(it.hasNext())
{
Course cr=(Course)it.next();
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 用for each方法访问集合元素
*/
public void testForEach()
{
for(Object obj:courseToSelect)
{
Course cr=(Course)obj;
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
public static void main(String[] args)
{
ListTest It=new ListTest();
It.testAdd();
It.testGet();
It.testIterator();

It.testForEach();
}

课程修改

public void testModify()
{
courseToSelect.set(4, new Course("6", "毛概"));
}

课程删除

public void testRemove()
{
Course cr=(Course)courseToSelect.get(4);
courseToSelect.remove(cr);
//删除4,5位置上的元素
Course[] courses={(Course)courseToSelect.get(4),(Course)courseToSelect.get(5)};
courseToSelect.removeAll(Arrays.asList(courses));
//转化成集合
testForEach();
}

应用泛型管理课程

集合中的元素,可以是任意类型的对象(对象的引用);泛型,用来存放特定类型元素的集合,会在编译期间进行类型检查,,可以直接按指定类型获取集合元素

泛型集合中只能添加泛型及其子类对象
泛型不能是基本类型,必须是使用其对应的包装类
格式:

List<Integer> a = new ArrayList<Integer>();
a.add(1);
System.out.println(""+a.get(0));

子类继承父类时,会默认构建一个无参的构造器,但是如果父类中没有无参的构造器就会报错,所以需要在父类中定义一个无参的构造方法。

-----------------------------------------------------------------------------------------------------------------------------------------------------------

public String id;
public String name;
//实际应该private,然后用get,set
//含参构造器
public Course(String id,String name){
this.id=id;
this.name=name;
}
Student.java
public String id;
public String name;
public Set courses;
//引入Set包
提供id,name就可以构造出学生
构造器
public Student(String id,String name){
this.id=id;
this.name=name;
this.courses=new HashSet();
}
由于Set接口不能实例化所以用HashSet对courses进行实例化
用list容器存放备选课程
ListTest.java
public class ListTest{
public List coursesToSelect;//引入java.util下的List包//List类型的属性
public ListTest(){//构造方法用于初始化coursesToSelect属性
this.coursesToSelect=new ArrayList();//引入ArrayList包
}
//往容器里添加课程,调用List里的方法
public void testAdd(){
Course cr1=new Course("1","数据结构");//调用了Course的构造器
CourseToSelect.add(cr1);
//Course temp=coursesToSelect.get(0);
//报错:对象存入集合会变成object类型,取出时需要进行类型转换
Course temp=(Course)courseToSelect.get(0);
System.out.println(temp.id+temp.name);
//todoList中重载的另一种方法,添加新元素,还可以指定位置

}
public static void main(){
ListTest lt=new ListTest();
lt.testAdd();//运行java运行方式
}
}
//todo
Course cr2=new Course("2","C语言");
coursesToSelect.add(0,cr2);
Course temp2=(Course)courseToSelect.get(0);
System.out.println(temp2.id+temp2.name);

//List中的元素是可重复的:
coursesToSelect.add(cr1);
Course temp0=(Course)coursesToSelect.get(2);
System.out.println(temp0.id+temp0.name);


//Course cr3=new Course("2","C++");
//coursesToSelect.add(4,cr3);
//异常:下标越界
Course[] course={new Course("3","离散"),new Course("4","汇编")};
coursesToSelect.addAll(arrays.asList(course));//Collection 的实例//把数组转变为List
Course temp3=(Course)courseToSelect.get(2);
Course temp4=(Course)courseToSelect.get(3);
System.out.println(temp3.id+temp3.name);

Course[] course2={new Course("5","高数"),new Course("6","英语")};
coursesToSelect.addAll(2,arrays.asList(course2));
Course temp5=(Course)courseToSelect.get(2);
Course temp6=(Course)courseToSelect.get(3);
}
public void testGet(){
int size= coursesToSelect.size(); //通过size()取得list的长度
System.out.println("待选课程:");
for(int i=0;i<size;i++){
Course cr=(Course)coursesToSelect.get(i);//取得相应位置上的元素
System.out.println("课程:"+cr.id+":"cr.name);
}
}
在main中调用lt.testGet();
List中的元素是可重复的:......

//通过迭代器遍历List//通过集合的iterator方法,取得迭代器的实例
public void testIterator(){//Iterator接口//导入包
Iterator it=coursesToSelect.iterator();
System.out.println("用迭代器遍历出的待选课程:");
while(it.hasNext){//如果还有元素为真
Course cr=(Course)it.next();//取元素
System.out.println("课程:"+cr.id+":"cr.name);
}
}
在main中调用lt.testIterator();
迭代器依赖于集合而存在

//通过for each()访问集合元素
public void testForEach(){
System.out.println("用for each遍历出的待选课程:");
for(Object obj:coursesToSelect){
System.out.println("课程:"+cr.id+":"cr.name);
}
}


课程修改
public void testModify(){
coursesToSlect.set(4,new Course("7","毛概"));
}
在main中调用 It.testModify();It.testForEach();

课程删除
public void testRemove()
{
//Course cr=(Course)coursesToSelect.get(4);//取得4位置上的元素
//System.out.println("我是将要被删除的课程:"+cr.id+cr.name);
//coursesToSelect.remove(cr);
//System.out.println("已删除课程"+cr);
coursesToSelect.remove(4);
testForEach();

//删除4,5位置上的元素
Course[] courses={(Course)coursesToSelect.get(4),(Course)coursesToSelect.get(5)};
coursesToSelect.removeAll(Arrays.asList(courses));//转化成集合
testForEach();
}
应用泛型管理课程
List里不能加不一样的类型对象
集合中的元素可以是任意类型的对象(对象的引用)
泛型:规定了某个集合只可以存放特定类型的对象,会在编译期间进行检查,可以直接按指定类型获取集合元素
新建TestGeneric.java
带有泛型的List写法:
/**
* 用于存放备选课程的List
*/
public class ListTest {
public List coursesToSelect;
public ListTest()
{
this.coursesToSelect=new ArrayList();
}
/**
* 在courseToSelect容器中添加课程
*/
public void testAdd() {
//创建一个课程对象,利用add方法
Course cr1=new Course("1", "数据结构");//课程对象的实例
coursesToSelect.add(cr1);
//Course temp=courseToSelect.get(0);
Course temp=(Course)coursesToSelect.get(0);
//对象存入集合都会变为object类型,取出时要作类型转换
System.out.println("添加了课程"+temp.id+":"+temp.name);

Course cr2=new Course("2", "C++");
coursesToSelect.add(0, cr2);
Course temp2=(Course)coursesToSelect.get(0);
System.out.println("添加了课程"+temp2.id+":"+temp2.name);

//List中的元素是可重复的:
coursesToSelect.add(cr1);
Course temp0 =(Course)coursesToSelect.get(2);
System.out.println(temp0.id+temp0.name);

//Course cr3=new Course("3", "C");
//courseToSelect.add(4, cr3);
//报异常数组下标越界 add是在可以访问的位置上插入

//创建课程数组添加两个元素
Course[] course ={new Course("3", "离散数学"),new Course("4", "汇编语言")};
coursesToSelect.addAll(Arrays.asList(course));
//addAll是collection
Course temp3=(Course)coursesToSelect.get(2);
Course temp4=(Course)coursesToSelect.get(3);

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);
}
/**
* 取得list中元素的方法
*/
public void testGet()
{
System.out.println("可选课程:");
for(int i=0;i<coursesToSelect.size();i++)
{
Course cr=(Course)coursesToSelect.get(i);
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/**
* 用迭代器遍历list
* @param args
*/
public void testIterator()
{
Iterator it=coursesToSelect.iterator();
System.out.println("用迭代器遍历出的待选课程:");
while(it.hasNext())
{
Course cr=(Course)it.next();
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/**
* 用for each方法访问集合元素
*/
public void testForEach()
{
System.out.println("用for each遍历出的待选课程:");
for(Object obj:coursesToSelect)
{
Course cr=(Course)obj;
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/**
* 修改list中的元素
*/
public void testModify()
{
coursesToSelect.set(4, new Course("7", "毛概"));
}
/**
* 删除List中的元素
* @param args
*/
public void testRemove()
{
//Course cr=(Course)coursesToSelect.get(4);//取得4位置上的元素
//System.out.println("我是将要被删除的课程:"+cr.id+cr.name);
//coursesToSelect.remove(cr);
//System.out.println("已删除课程"+cr);
coursesToSelect.remove(4);
testForEach();

//删除4,5位置上的元素
Course[] courses={(Course)coursesToSelect.get(4),(Course)coursesToSelect.get(5)};
coursesToSelect.removeAll(Arrays.asList(courses));//转化成集合
testForEach();
}
public static void main(String[] args)
{
ListTest It=new ListTest();
It.testAdd();
It.testGet();
It.testIterator();
It.testForEach();
It.testModify();It.testForEach();
It.testRemove();
}
}
泛型的子类型对象实例
新建ChildCourse.java继承Course
//报错,Course中有含参构造器,编译器就不会为其添加隐式构造器,而子类中必须调用父类的隐式构造器,所以在Course中添加无参构造器public Course(){}

TestGeneric中测试泛型集合介意添加泛型的子类型的对象实例
public void testChild(){
ChildCourse ccr=new ChildCourse();
ccr.id="3";
ccr.name="我是子类型的课程对象实例";
courses.add(ccr);
}
在main中调用tg.testChild(); tg.testForEach();

泛型集合中的限定类型不能使用基本数据类型,可通过使用包装类限定允许存入的基本数据类型
例如:int~Integer long~Long boolean~Boolean
public void testBasicType()
{
//List<int> list =new ArrayList<int>();
List<Integer> list =new ArrayList<Integer>();
list.add(1);//被强制包装成Integer类型
System.out.println("基本类型必须使用包装类作为泛型"+list.get(0));

}


通过Set集合管理学生课程
不能修改指定位置的元素(无序 )

Set类型的属性:courses

在Student.java中修改public Set courses;
为public Set<Course> courses;
构造器中加泛型this.courses=new HashSet<Course>();
新建SetTest.java
public List<Course> coursesToSelect;
public SetTest()
{
coursesToSelect=new ArrayList<Course>();
}
public void testAdd() {
Course cr1=new Course("1", "数据结构");//课程对象的实例
coursesToSelect.add(cr1);
Course temp=(Course)coursesToSelect.get(0);

Course cr2=new Course("2", "C++");
coursesToSelect.add(0, cr2);
Course temp2=(Course)coursesToSelect.get(0);

Course[] course ={new Course("3", "离散数学"),new Course("4", "汇编语言")};
coursesToSelect.addAll(Arrays.asList(course));
Course temp3=(Course)coursesToSelect.get(2);
Course temp4=(Course)coursesToSelect.get(3);
}
public void testForEach()
{
for(Object obj:coursesToSelect)
{
Course cr=(Course)obj;
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
public static void main(String[] args){
SetTest st=new SetTest();
st.testAdd();
st.testForEach();
//学生对象
Student s=new Student("1","小米");
System.out.println("请选课"+s.name);

//建一个Scanner对象,用来接收从键盘输入的课程id
Scanner console=new Scanner(System.in);

for(int i=0;i<3;i++)
{
System.out.println("请输入课程ID");
String courseId=console.next();
for(Course cr:st.coursesToSelect){
if(cr.id.equals(courseId)){
s.courses.add(cr);
//Set保留最后一次添加的那个,可以添加null
}
}
}
st.testForEach(s);
}
public void testForEach(Student s){
for(Course cr:s.courses){
System.out.println("已选课程:"+cr.id+":"+cr.name);
}
}

mooc_学生所选课程

标签:

原文地址:http://www.cnblogs.com/mao-19/p/4897887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!