为了账号安全,请及时绑定邮箱和手机立即绑定

tesGet出错?

http://img1.sycdn.imooc.com//57a75721000174c913660768.jpg

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();

}

/**

* 用于王coursesToSelesect中添加备选课程

*/

     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","JAVA入门到转行");

    coursesToSelect.add(0,cr3);

    Course temp3 = (Course) coursesToSelect.get(0);

    System.out.println("添加了课程"+temp3.id+":"+temp3.name+"");

    /*

     * 以下会抛出数组下标异常

     *  Course cr3 = new Course("3","JAVA入门到转行");

     *  coursesToSelect.add(4,cr3);

     */

     

    Course[] course = {new Course("4","离散数学"),new Course("5","汇编语言")};

    coursesToSelect.addAll(Arrays.asList(course));

    Course temp4 = (Course)coursesToSelect.get(3);

    Course temp5 = (Course)coursesToSelect.get(4);

    System.out.println("添加了两门课程"+temp4.id+":"+temp4.name+"和"+temp5.id+":"+temp5.name+"");

   

    Course[] course2 = {new Course("6","高等数学"),new Course("7","大学英语")};

    coursesToSelect.addAll(2, Arrays.asList(course2));

    Course temp6 = (Course)coursesToSelect.get(2);

    Course temp7 = (Course)coursesToSelect.get(3);

    System.out.println("添加了两门课程"+temp6.id+":"+temp6.name+"和"+temp7.id+":"+temp7.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);

        }

         

         } 

     }

     public static void main(String[] args){

    ListTest lt = new ListTest();

    lt.testAdd();

    lt.testGet();

     }

}

如图,代码会出现这种错误,这里暂时只贴出主函数,其他类确保和老师的一样了。我该怎么解决?

正在回答

2 回答

你的花括号好像对应错了

0 回复 有任何疑惑可以回复我~
#1

慕粉3586557 提问者

我已经找到了,谢谢
2016-08-08 回复 有任何疑惑可以回复我~

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


 class Course {
 
 public String id;
 public String name;
 
 public Course(String id,String name){
  
  this.id=id;
  this.name=name;
  
 }

}

/*

 * 备选课程类

 */

public class ListTest {

/**

* 用于存放备选课程的List

*/

public List coursesToSelect;

public ListTest(){

this.coursesToSelect=new ArrayList();

 

}

/**

* 用于王coursesToSelesect中添加备选课程

*/

     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","JAVA入门到转行");

    coursesToSelect.add(0,cr3);

    Course temp3 = (Course) coursesToSelect.get(0);

    System.out.println("添加了课程"+temp3.id+":"+temp3.name+"");

    /*

     * 以下会抛出数组下标异常

     *  Course cr3 = new Course("3","JAVA入门到转行");

     *  coursesToSelect.add(4,cr3);

     */

    

    Course[] course = {new Course("4","离散数学"),new Course("5","汇编语言")};

    coursesToSelect.addAll(Arrays.asList(course));

    Course temp4 = (Course)coursesToSelect.get(3);

    Course temp5 = (Course)coursesToSelect.get(4);

    System.out.println("添加了两门课程"+temp4.id+":"+temp4.name+"和"+temp5.id+":"+temp5.name+"");

  

    Course[] course2 = {new Course("6","高等数学"),new Course("7","大学英语")};

    coursesToSelect.addAll(2, Arrays.asList(course2));

    Course temp6 = (Course)coursesToSelect.get(2);

    Course temp7 = (Course)coursesToSelect.get(3);

    System.out.println("添加了两门课程"+temp6.id+":"+temp6.name+"和"+temp7.id+":"+temp7.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);

        }

        

         }

    

     public static void main(String[] args){

    ListTest lt = new ListTest();

    lt.testAdd();

    lt.testGet();

     }

}

试试

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409792    人
  • 解答问题       4340    个

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

进入课程

tesGet出错?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信