Alan恍恍惚惚
2015-07-28 10:20
package jihe;
import java.util.*;
/**
* 备选课程类
* @author Alan
*
*/
public class ListText {
/**
* 用于存放备选课程的List
*/
public List coursesToSelect;
public ListText(){
this.coursesToSelect = new ArrayList();
}
/**
* 用于往coursesToSelect中添加备选课程
*/
public void testAdd(){
Course c1 = new Course("1","数据结构");
coursesToSelect.add(c1);
Course temp =(Course)coursesToSelect.get(0);
System.out.println("1添加了课程:"+temp.id+":"+temp.name);
Course c2 =new Course("2","Java");
coursesToSelect.add(0,c2);
Course temp1= (Course)coursesToSelect.get(0);
System.out.println("2添加了课程:"+temp1.id+":"+temp1.name);
Course c3 =new Course("3","C语言");
coursesToSelect.add(0,c3);
Course temp2= (Course)coursesToSelect.get(0);
System.out.println("3添加了课程:"+temp2.id+":"+temp2.name);
Course[] course = {new Course("4","线性代数"),new Course("5","C#") };
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);
}
/**
* 使用set方法修改List中的值
* @param args
*/
public void testModify(){
coursesToSelect.set(4,new Course("8","毛概"));
}
//排列输出
public void textGet(){
int size = coursesToSelect.size();
for(int i=0;i<size;i++){
Course cr=(Course)coursesToSelect.get(i);
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
//使用迭代器iterable进行排列
public void testIterator(){
Iterator it = coursesToSelect.iterator();
//使用iterator的hasNext方法实现循环
System.out.println("有以下可选课程(使用迭代器循环):");
while(it.hasNext()){
Course cr = (Course)it.next();
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
public static void main(String[] args) {
ListText lt1 = new ListText();
lt1.testAdd();
lt1.textGet();
lt1.testIterator();
lt1.testModify();
}
}
你是修改了,可是没输出修改后的结果。
Java入门第三季
409792 学习 · 4340 问题
相似问题