[학습 목표]
1. 자바의 배열 구조를 통해 참조형 자료들을 처리하는 것을 확인 하고 활용할 수 있다. 2. 일차원, 다차원, 객체 배열 등을 통해 데이터를 프로세싱할 수 있다. 3. record 개념과 클래스 구현 방법을 이해하고 클래스 기능을 추가할 수 있다. 4. 상속의 개념과 오버라이딩, 오버로딩을 활용해서 기능 구현, 선언 , 활용을 자유롭게 할 수 있다. |
배열
1) 자바의 배열 구조 살펴보기
- 일차원 배열 -
int[] ar = {10, 20 , 30, 40, 50 }; -> 나열형 상수로 배열의 메모리 확보 후 시작 주소 리턴한다.
int[] ar = new int [] {10,20,30,40,50}; -> 나열형 상수로 배열의 동적할당 후 시작 주소 리턴한다 = 나열된 값으로 초기화
int[] ar = new int[10]; -> 10개의 정수를 동적할당 배열로 메모리 생성한 후 시작 주소 리턴한다 = 기본값으로 초기화
- 다차원 배열 -
int ar[][] = {{10, 20 , 30},{ 40, 50, 60 }}; -> 나열형 상수로 배열의 메모리 확보 후 시작 주소 리턴한다.
int[] ar[] = new int [2][]{{10,20,30},{40,50,60}}; -> 나열형 상수로 배열의 동적할당 후 시작 주소 리턴한다. = 나열된 값으로 초기화
int[][] ar = new int [2][3]; -> 2 * 3개의 정수를 동적할당 배열로 메모리 생성한 후 시작주소 리턴한다. = 기본값으로 초기화
일차원 배열과 다차원 배열에서의 첫 번째와 같은 경우는 자유 영역 공간 할당이 아닌 static , stack 영역에 할당되어 추천하지 않는다.
2) 자바의 배열 구조를 통해 참조형 자료들을 처리하는 과정 살펴보기
ex) int[] ar[] = new int [2][]{{10,20,30},{40,50,60}};
배열을 생성하면 ar이라는 방이 stack , static 공간에 생긴다. 자유 영역 공간 할당에서 2개의 행에 대한 주소값을 생성한다. ar은 이의 시작 주소를 참조한다. 그리고 ar[0] 과 ar[1]은 각각 열에 대한 주소값을 생성한 후 이들의 시작 주소를 참조한다.
3) 배열 관련 주요 메소드
- 배열 복사를 위한 System.arraycopy 활용: System.arraycopy(source, srcIndex, destination, destIndex, length);
- 배열 전체 복사 및 초기화 : int[] copy = Arrays.copyOf ( ar, ar.length); / Arrays.fill(ar, 0) -> 모든 요소를 0으로 초기화
- 배열 비교 및 정렬 : boolean isEqual = Arrays.equals(ar, copy); / Arrays.sort(ar);
- 배열을 List로 변환: -> 일괄 처리를 컬렉션의 객체를 통해 빠르게 처리하기 위함
List<Integer> list = List.of(1, 2, 3, 4, 5);
Integer[] array = list.toArray(new Integer[0]);
- Stream API [ 연산자 , 제어문 -> 메소드 ] 활용한 배열 변환:
int[] numbers = {1, 2, 3, 4, 5};
int sum = Arrays.stream(numbers).sum();
4) Collection
[링크 참조]
https://docs.oracle.com/javase/8/docs/api/index.html
index – List -> 중복관리 , null 허용
value – Set -> 중복 x, null 허용
key - Map < key , value > -> key 중복 관리 , null 허용 / value x
다차원 배열
1) 다차원 배열 개념 및 선언
- 2차원 배열 선언 방식:
int[][] arr = new int[2][3];
int[][] arr = {{10, 20, 30}, {40, 50, 60}};
- 행렬의 전치(transpose matrix) 구현 예제:
int[][] TA = new int[arr[0].length][arr.length];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
TA[j][i] = arr[i][j];
}
}
2) 가변 배열 ( Jagged Array )
- 가변 배열 개념 및 선언
요소의 크기가 서로 다른 배열을 저장할 수 있다.
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[2];
jaggedArray[1] = new int[3];
jaggedArray[2] = new int[1];
record
1) record 선언
ex)
record Address(String name , String addr , String tel) { } // record 는 불변 객체임 , readable만 가능 , set은 불가능
public class MTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Address a1 = new Address("홍길동","서울","02-000");
System.out.println(a1.tel());
System.out.println(a1.name());
}
}
2) Patter Matching for switch
객체가 특정 배열 타입인지 검사 가능
Object obj = new int[]{1, 2, 3};
switch (obj) {
case int[] arr -> System.out.println("This is an int array: " + Arrays.toString(arr));
default -> System.out.println("Unknown type");
}
상속
1) 상속
기능확장 목적이다.
//AA.java
public class AA {
private int a ;
private int b;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public int getHap() {
return getA() + getB();
}
}
//BB.java
public class BB extends AA {
private int c;
public void setC(int c) {
this.c = c;
}
public int getC() {
return c;
}
public int getRes() {
return super.getHap() - getC();
}
}
//DD.java
public class DD extends BB {
private int d;
public int getD() {
return d;
}
public void setD(int d) {
this.d = d;
}
public int getRes() {
return super.getRes() * getD();
}
public static void main(String[] args) {
AA d1 = new AA (); //암묵적으로 Object , AA() ,BB() ,DD() 객체 생성됨
d1.setA(100);
d1.setB(50);
d1.setC(5);
d1.setD(2);
String res = String.format("(%d + %d) - %d * %d = %d\n",d1.getA(), d1.getB(), d1.getC() , d.getD(),d1.getRes());
System.out.println("result: "+ d1.getRes());
}
}
'🦁멋쟁이 사자처럼 15기 > 3월달 수업 내용 정리' 카테고리의 다른 글
멋쟁이 사자처럼 12회 ( 03 / 13 ) (0) | 2025.03.13 |
---|---|
멋쟁이 사자처럼 11회차 ( 03 / 12 ) (0) | 2025.03.12 |
멋쟁이 사자처럼 10회차 ( 03 / 11 ) (0) | 2025.03.11 |
멋쟁이 사자처럼 8회차 ( 03 / 07 ) (0) | 2025.03.07 |
멋쟁이 사자처럼 7회차 ( 03 / 06 ) (0) | 2025.03.06 |