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

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

報(bào)Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4錯(cuò)誤是什么原因

我照著打的,不知道啥問題,求大神解答

正在回答

4 回答

越界了 ? 仔細(xì)對(duì)比一下 ?改一改 ? ? 視頻中 ?老師動(dòng)作太快 ? 沒跟上。。。。仔細(xì)看看吧 ? 有地方是原來(lái)的代碼

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

weibo_愛吃蔬菜的兔八哥_04192614 提問者

非常感謝!
2016-11-20 回復(fù) 有任何疑惑可以回復(fù)我~

沒實(shí)例化吧

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

?Course[] course={new Course("3","離散數(shù)學(xué)"),new Course("4","匯編語(yǔ)言")};

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

? ?Course temp3=(Course) coursesToSelect.get(2);

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

? ?//System.out.println("添加了兩個(gè)課程:"+temp3.id+":"+temp3.name+","+temp4.id+":"+temp4.name);


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

package com.imooc.map_demo;

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

public class SetTest {

?? ?public List<Course> coursesToSelect;
?? ?
?? ?public SetTest(){
?? ??? ?coursesToSelect = new ArrayList<Course>();
?? ?}
?? ?
?? ?
?? ? /**
???? * 用于往courseToSelect中添加備選課程
???? */
??? public void testAdd(){
?? ??? ?//創(chuàng)建一個(gè)課程對(duì)象,通過(guò)調(diào)用add方法,添加到備選課程list中
?? ??? ?Course cr1=new Course("1","數(shù)據(jù)結(jié)構(gòu)");
?? ??? ?coursesToSelect.add(cr1);
?? ??? ?Course temp=(Course) coursesToSelect.get(0);
?? ??? ?//System.out.println("添加了課程:"+temp.id+":"+temp.name);
?? ??? ?
?? ??? ?Course cr2=new Course("2","C語(yǔ)言");
?? ??? ?coursesToSelect.add(0, cr2);
?? ??? ?Course temp2=(Course) coursesToSelect.get(0);
?? ??? ?//System.out.println("添加了課程:"+temp2.id+":"+temp2.name);
?? ??? ?
?? ??? ?
//?? ??? ?coursesToSelect.add(cr1);
//?? ??? ?Course temp0=(Course) coursesToSelect.get(2);
//?? ??? ?System.out.println("添加了課程:"+temp.id+":"+temp.name);
?? ??? ?//以下方法會(huì)拋出數(shù)組下標(biāo)越界異常
//?? ??? ?Course cr3=new Course("3","test");
//?? ??? ?coursesToSelect.add(4, cr3);
?? ??? ?
?? ??? ?Course[] course={new Course("3","離散數(shù)學(xué)"),new Course("4","匯編語(yǔ)言")};
?? ??? ?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);

????????? Course[] course2={new Course("5","高等數(shù)學(xué)"),new Course("6","大學(xué)英語(yǔ)")};
????????? 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);
??? }
?? ?
??? /**
???? * 通過(guò)for each 方法訪問集合元素
???? * @param args
???? */
??? public void testForEach(){
?? ??? ?System.out.println("有如下課程待選(通過(guò)for each訪問):");
?? ??? ?for(Object obj:coursesToSelect){
?? ??? ??? ?Course cr=(Course) obj;
?? ??? ??? ?System.out.println("課程:"+cr.id+":"+cr.name);
?? ??? ?}
??? }
?? ?
?? ?public static void main(String[] args) {
?? ??? ?SetTest st=new SetTest();
?? ??? ?st.testAdd();
?? ??? ?st.testForEach();
?? ??? ?//創(chuàng)建一個(gè)學(xué)生對(duì)象
?? ??? ?Student student =new Student("1","小明");
?? ??? ?System.out.println("歡迎學(xué)生:"+student.name+"選課!");
?? ??? ?//創(chuàng)建一個(gè)scanner對(duì)象,用來(lái)接收從鍵盤輸入的ID
?? ??? ?
?? ??? ?Scanner console=new Scanner(System.in);
?? ??? ?
?? ??? ?for(int i=0;i<3;i++){
?? ??? ??? ?System.out.println("請(qǐng)輸入課程ID");
?? ??? ??? ?String courseId=console.next();
?? ??? ??? ?for(Course cr:st.coursesToSelect){
?? ??? ??? ??? ?if(cr.id.equals(courseId)){
?? ??? ??? ??? ??? ?student.courses.add(cr);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?st.testForEachForSet(student);
?? ?}
?
?? ?public void testForEachForSet(Student student){
?? ??? ?//打印輸出,學(xué)生所選的課程
?? ??? ??? ??? ?for(Course cr:student.courses){
?? ??? ??? ??? ??? ?System.out.println("選擇了課程:"+cr.id+":"+cr.name);
?? ??? ??? ??? ?}
?? ?}
?? ?
}


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

舉報(bào)

0/150
提交
取消

報(bào)Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4錯(cuò)誤是什么原因

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

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

幫助反饋 APP下載

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

公眾號(hào)

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