728x90
반응형
1. 배열
- 같은 타입의 여러 변수를 하나의 묶음으로 저장하는 '저장소 객체'
* 선언방법
- 타입 [] 변수이름
- 타입 변수이름 []
int [] array;
String str [];
* 생성
- new 타입[크기]
new int[3];
new String[5];
* 초기화
array[0] = 10;
str[3] = "문자열";
* 선언 + 생성
int [] array = new int[3];
String str [] = new String[5];
* 선언 + 생성 + 초기화 ( { } 사용)
int [] array = new int[] {100, 200, 300};
String str [] = {"문자열", "문자열1", "문자열2"};
* 다차원 배열
class D {
int is[] = {10, 20};
int iss[][] = {is, {30, 40, 50}, {60}, {70, 80, 90, 100}};
int isss[][][] = {iss, {{110, 120}, {130}}};
int issss[][][][] = {isss, { {{140, 150}, {160}}, {{170, 180}, {190}, {200}} } };
void output1() { // 이차원배열 출력
for(int i=0; i< iss.length; i++)
for(int j=0; j< iss[i].length; j++)
System.out.println( "iss[" + i + "]" + "[" + j + "]: " + iss[i][j]);
}
void output2() { // 삼차원배열 출력
for(int i=0; i< isss.length; i++) // 3차원 길이
for(int j=0; j< isss[i].length; j++) // 2차원 길이
for(int k=0; k< isss[i][j].length; k++) // 1차원 길이
System.out.println( "isss[" + i + "]" + "[" + j + "]" + "[" + k +"]: "
+ isss[i][j][k]);
}
void output3() { // 사차원배열 출력
for(int i=0; i< issss.length; i++)
for(int j=0; j< issss[i].length; j++)
for(int k=0; k< issss[i][j].length; k++)
for(int l=0; l< issss[i][j][k].length; l++)
System.out.println( "issss[" + i + "]" + "[" + j + "]" + "[" + k +"]"
+ "[" + l + "]: " + issss[i][j][k][l]);
}
public static void main(String[] args) {
D d = new D();
//d.output1();
//System.out.println("");
//d.output2();
d.output3();
}
}
-----
issss[0][0][0][0]: 10
issss[0][0][0][1]: 20
issss[0][0][1][0]: 30
issss[0][0][1][1]: 40
issss[0][0][1][2]: 50
issss[0][0][2][0]: 60
issss[0][0][3][0]: 70
issss[0][0][3][1]: 80
issss[0][0][3][2]: 90
issss[0][0][3][3]: 100
issss[0][1][0][0]: 110
issss[0][1][0][1]: 120
issss[0][1][1][0]: 130
issss[1][0][0][0]: 140
issss[1][0][0][1]: 150
issss[1][0][1][0]: 160
issss[1][1][0][0]: 170
issss[1][1][0][1]: 180
issss[1][1][1][0]: 190
issss[1][1][2][0]: 200
* 모든 인덱스는 0번부터 시작
2. 헤테로지니어스 (Heterogeneous, 이질화)
- 같은 타입으로 인스턴스화 하는 것을 호모지니어스(Homogeneous,동질화:배열같이 같은 타입의 저장) 라고 한다
- 두 클래스 사이에 상속관계가 존재해야 하며 부모의 인스턴스 변수를 자식타입으로 인스턴스화가 가능하다 (반대는 불가능)
- 부모타입의 인스턴스 변수가 접근할 수 있는 범위
ㄴ 부모 자신에 있는 멤버
ㄴ 자식에서 오버라이딩 한 메소드
class Parent {
void print() {
System.out.println("부모");
}
void a() {}
}
class Child extends Parent {
void print() {
System.out.println("자식");
}
void b() {}
}
class Main {
public static void main(String[] args) {
Parent parent = new Child(); // 이질화
Child child = new Parent(); // [x] 절대 불가
parent.print(); // 부모것이 아닌 자식것이 호출된다
parent.a(); // 갖고 있는 메소드 명이 다르면 부모것이 호출된다
// 자식에게 접근하고 싶으면 형변환 해줘야된다
((Child)parent).b();
Child child = (Child)parent;
child.b();
}
}
728x90
반응형
'개발 > JAVA' 카테고리의 다른 글
18. JAVA 클래스간 형변환 / 업캐스팅 / 다운캐스팅 / 내부클래스 (InnerClass) (0) | 2020.06.08 |
---|---|
17. JAVA 인터페이스 (Interface) / 컬렉션 (Collection) (0) | 2020.06.08 |
15. JAVA 패키지 / 패키지 배포 (0) | 2020.06.08 |
14. JAVA 추상화 / 캡슐화 / 은닉화 / 생성자 / 싱글톤 (0) | 2020.06.08 |
13. JAVA 상속성 / 오버로딩(OverLoading) / 오버라이딩(OverRiding) / this / super (0) | 2020.06.08 |