package com.imooc.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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);//这是看是否插入进去了,提出来的用get方法
System.out.println("添加了课程"+temp.id+":"+temp.name);
Course cr2=new Course("2","c语言");
coursesToSelect.add(0, cr2);
Course temp2=(Course) coursesToSelect.get(0);
//存入忽略原来类型只当是object,取出也是object,所以要强转成课程类型
System.out.println("添加了课程"+temp2.id+":"+temp2.name);
//这次是用数组的方法添加,再把数组转化成列表,的addall方式
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);
public static void main(String[] args) {
ListTest It=new ListTest();
It.testAdd();
}
}
相关分类