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

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

為何會(huì)越界異常

package com.imooc.collection;


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

}

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

// System.out.println("添加了課程:" + temp2.id + ":" +temp2.name);

// coursesToSelect.add(cr1);

// Course temp0 = (Course) coursesToSelect.get(0);

// 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("添加了2門(mén)課程" + 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("添加了兩門(mén)課程"+ temp5.id + ":" +temp5.name +

// temp6.id + ":" + temp6.name);

}


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

// TODO Auto-generated method stub

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)接收用鍵盤(pán)輸入的課程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);

}

}

}


正在回答

1 回答

把這句注釋掉就好了~?

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



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

舉報(bào)

0/150
提交
取消

為何會(huì)越界異常

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

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

幫助反饋 APP下載

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

公眾號(hào)

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