-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArrayIndexOutOfBounds.java
More file actions
42 lines (39 loc) · 1.28 KB
/
ArrayIndexOutOfBounds.java
File metadata and controls
42 lines (39 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
HWJava19_07_ArrayIndexOutOfBounds_배재연.java
>> StudentDTO
String no;
String name;
getter / setter / Construct(_, _)
>> NullPointerStudent.java
main()
StudentDTO[] dto = StudentDTO 4개;
dto[0] = new StudentDTO("STU001", "뽀오");
dto[1] = new StudentDTO("STU002", "나나");
for(dto.length)
getter 이용 no, name 출력 // dto[2], dto[3]은 null 포인터이므로 오류남.
*/
package classes;
class ArrayIndexOutOfBounds {
public static void main(String[] args) {
StudentDTO[] dto = {new StudentDTO("STU001", "뽀오"),
new StudentDTO("STU002", "나나"),
new StudentDTO("STU003", "뚜비"),
new StudentDTO("STU004", "보라")};
System.out.print("학번\t");
System.out.println("이름");
for(int i = 0; i <= dto.length; i++) { // 0 1 2 3 4 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at classes.ArrayIndexOutOfBounds.main(ArrayIndexOutOfBounds.java:29)
try {
System.out.print(dto[i].getNo() + "\t");
System.out.println(dto[i].getName() + "\t");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println(i + "번째 ArrayIndexOutOfBounds 확인!!!");
}
}
/*
for(StudentDTO temp : dto){
System.out.print(temp.getNo()+"\t");
System.out.println(temp.getName() + "\t");
*/
}
}