郑荣
2015-08-20 17:57
package com.imooc.collection; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @author 荣 * */ public class ListTest { public List coursesToSelect; public ListTest(){ this.coursesToSelect = new ArrayList(); } public void testAdd(){ 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语言"); coursesToSelect.add(0, cr2); Course temp2 = (Course)coursesToSelect.get(0); System.out.println("添加了课程" + temp2.id + ":" + temp2.name); // 抛出数组下标越界异常 // Course cr3 = new Course("3","test"); // coursesToSelect.add(6, cr3); // Course temp3 = (Course) coursesToSelect.get(2); // System.out.println("添加了课程" + temp3.id + ":" + temp3.name); 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); 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); public void testGet(){ int size = coursesToSelect.size(); System.out.println(); System.out.println("有如下课程待选:"); for(int i = 0;i<size;i++){ Course cr = coursesToSelect.get(i); System.out.println("课程:" + cr.id + ":" + cr.name); } } } public static void main(String[] args){ ListTest lt = new ListTest(); lt.testAdd(); lt.testGet(); } }
49行和63行会提示错误
你检查一下,你所有符号闭合了没?
Java入门第三季
409792 学习 · 4340 问题
相似问题