第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

集合框架學(xué)生選課提示數(shù)組下標(biāo)越界

按照老師說的輸入的提示:


這個(gè)提示是數(shù)組下標(biāo)越界的意思吧,但是我是按照視頻里代碼輸入的,包括導(dǎo)入包都沒有錯(cuò)誤,不知道為什么出現(xiàn)這個(gè)提示。

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 ? ?


不知道為什么?

正在回答

2 回答

Course cr1 = new Course("1","數(shù)據(jù)結(jié)構(gòu)");

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

coursesToSelect.add(cr1);

順序錯(cuò)了,代碼執(zhí)行是按照寫的順序來的,你還沒加進(jìn)去就要取出來,肯定不對(duì),把?coursesToSelect.add(cr1); 跟

Course temp = (Course) coursesToSelect.get(0);調(diào)換一下順序就可以了。

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

清澄白河 提問者

十分感謝!
2017-05-26 回復(fù) 有任何疑惑可以回復(fù)我~

寫的代碼如下:

package com.imooc.collection;


/**

?* 課程類

?* @author Administrator

?*

?*/

public class Course {


public String id;

public String name;

public Course(String id,String name){

this.id = id;

this.name = name;

}

}

=================================================

package com.imooc.collection;


import java.util.HashSet;

import java.util.Set;

/**

?* 學(xué)生類

?* @author Administrator

?*

?*/

public class Student {

public String id;

public String name;

public Set courses;


public Student(String id,String name){

this.id = id;

this.name = name;

this.courses = new HashSet();

}

}

======================================

package com.imooc.collection;


import java.util.List;

import java.util.ArrayList;


/**

?* 備選課程類

?* @author Administrator

?*

?*/

public class ListTest {


/**

* 用于存放備選課程的list

*/

public List coursesToSelect;

public ListTest(){

this.coursesToSelect = new ArrayList();

}

/**

* 用于往coursesToSelect中添加備選課程

*/

public void testAdd(){

//創(chuàng)建一個(gè)課程對(duì)象,并且通過調(diào)用add方法,添加到備選課程list中

Course cr1 = new Course("1","數(shù)據(jù)結(jié)構(gòu)");

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

coursesToSelect.add(cr1);

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

}

public static void main(String[] args){

ListTest lt = new ListTest();

lt.testAdd();

}

}


1 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

集合框架學(xué)生選課提示數(shù)組下標(biāo)越界

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)