vector클래스
자바의 배열은 고정길이를 사용함. 즉, 배열의길이를 수정할수없다. Vector클래스는 가변길이의 배열이라고 할수있다.
vector클래스는 객체에대한 참조값을 저장하는 배열 다양한 객체들이 하나의 vector에 저장될수있고, 길이에 따라 증감한다.
Vector{} : 10개의 데이터를 저장할수있게 생성한다. 저장공간이 부족한경우 10개씩 증가한다. Vector(int size) : size개의 데이터를 저장할수있는 길이의 객체를 생성한다 Vector(int size, int incr) : size개의 데이터를 저장할수있는 길이의 객체를 생성한다.
Object get(int index) : 지정된 위치의 객체를 반환한다. 반환타입이 object타입이므로 반드시 형변환을 해줘야한다.
vector 시작주소값을 담기때문에 여러참조값을 저장할수있다.
추상클래스
하나이상의 추상 메소드를 포함하는 클래스
추상메소드 : 자식클래스에서 반드시 오버라이딩 해야만 사용할수있는 메소드 추상클래스는 추상메소드를 포함하고있다는점 이외에는 일반클래스와 모든점이 같다.
자식이무조건 구현을해야할때 사용한다.
this() 의 의미가이해가안갔다. 이건 같은클래스의 다른생성자를 호출할때사용한다.
예외처리
프로그램이 실행중 오작동을 하거나 비정상적으로 종료되는경우 프로그램 에러라고한다. 대부분 실행시에 발생하는 에러를 잡는것이다.
예외가 발생하더라도 프로그래머가 이에 대한 적절한 코드를 미리 작성해 높아 프로그램이 비정상적으로 종료되는 것을 막는 것을 의미
예시
1
2
3
4
5
6
7
8
9
10
try {
//오류 처리하는 코드
}
CATCH(Exception e1){
}
FINALLY {
//무조건 실행되는 코드
//예) DB CONNECTION시 오류가생길때, DB를 끊어주는 위치
}
예외처리1 ArithmeticException
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void main(String[] args)
{
int number = 100;
int result = 0;
int i;
for(i=0; i<10; i++)
{
try
{
result = number / (int)(Math.random()* 10);
System.out.println(result);
}catch(ArithmeticException e)
{
System.out.println("0 으로 나누었습니다.");
}
}
}
예외처리2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void main(String[] args)
{
System.out.println(1);
System.out.println(2);
try
{
System.out.println(3);
System.out.println(3/0);
System.out.println(4);
}
catch(Exception e)
{
System.out.println(5);
}
System.out.println(6);
}
예외처리3 printStackTrace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static void main(String[] args)
{
System.out.println(1);
System.out.println(2);
try
{
System.out.println(3);
System.out.println(3/0);
System.out.println(4);
}
catch(ArithmeticException ae) //작은 범위의 오류
{
System.out.println(4.5);
ae.printStackTrace(); //오류를 프린트하여
System.out.println(ae.getMessage()); //오류를 메시지처리한다.
}
catch(Exception e) //큰 범위의 오류
{
System.out.println(5);
}
System.out.println(6);
}
예외처리 4 Exception
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void main(String[] args)
{
//예외 발생시키기
try
{
//Exception e = new Exception("고의로 발생시켰음."); //exception객체를 생성해서
//throw e;
throw new Exception("고의로 발생시켰음.");
}
catch(Exception e)
{
System.out.println("에러 메시지 : " + e.getMessage());
e.printStackTrace();
}
System.out.println("프로그램이 정상 종료되었습니다.");
}
예외처리
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void main(String[] args) throws Exception
{
//메서드에 예외처리 선언
method1();
}
static void method1() throws Exception
{
method2();
}
static void method2() throws Exception
{
throw new Exception(); //나를 호출한 것에 던진다.
}
try catch finally
예외로 return이 있어도 try catch finally 문을 다타고 들어간다
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package sample;
public class Sample3 {
public static void main(String[] args) {
// 사용자정의 예외 만들기
try
{
startInstall();
copyFile();
}
catch(SpaceException e) // 여기서 오류가 생길경우 아래 오류문은 거치지않는다.
{
System.out.println("에러 메세지 : " + e.getMessage());
e.printStackTrace();
System.out.println("공간이 확보후 설치하세요.");
}
catch(MemoryException me)
{
System.out.println("에러 메세지 : " + me.getMessage());
me.printStackTrace();
System.out.println("공간이 dd확보후 설치하세요.");
}
finally
{
deleteTempFiles();
}
}
static void startInstall() throws SpaceException, MemoryException
{
if(!enoughSpace())
{
throw new SpaceException("설치할 공간 부족.");
}
if(!enoughMemory())
{
throw new SpaceException("메모리 부족.");
}
}
static void copyFile()
{
//파일 복사
System.out.println("copyFile() 파일복사 dd.");
}
static void deleteTempFiles()
{
//임시파일 삭제
System.out.println("deleteTempFiles() 임시파일 삭제.");
}
static boolean enoughSpace()
{ //설치시 필요한 공간 체크
return true;
}
static boolean enoughMemory()
{
return true;
}
}
class SpaceException extends Exception // 예외를 상속받았다.
{
SpaceException(String msg) //생성자
{
super(msg);
}
}
class MemoryException extends Exception
{
MemoryException(String msg)
{
super(msg);
}
}