Chapter 1: 배열 개요
배열이란?
배열은 동일한 타입의 데이터를 저장하는 연속된 공간입니다.
int[] numbers = new int[5];
Java에서 배열 표기법
int[] arr1 = new int[10];
int arr2[] = new int[10];
배열의 차원
int[][] matrix = new int[3][3];
배열 요소에 접근
int[] numbers = {1, 2, 3};
System.out.println(numbers[0]); // 1
배열의 경계 검사
if (index >= 0 && index < numbers.length) {
System.out.println(numbers[index]);
}
배열과 컬렉션 비교
- 배열: 고정 크기
- 컬렉션: 동적 크기 지원
Chapter 2: 배열 생성
배열 인스턴스 생성
int[] arr = new int[5];
배열 요소 초기화
int[] arr = {1, 2, 3, 4, 5};
다차원 배열 요소 초기화
int[][] matrix = {{1, 2}, {3, 4}};
가변 길이 배열
int[][] jagged = new int[3][];
jagged[0] = new int[2];
jagged[1] = new int[3];
계산된 크기를 가진 배열 생성
int size = 10;
int[] dynamicArray = new int[size];
배열 요소 복사
int[] source = {1, 2, 3};
int[] destination = Arrays.copyOf(source, source.length);
Chapter 3: 배열 사용
배열의 크기
int[] arr = {1, 2, 3};
System.out.println(arr.length);
배열 메소드
Arrays.sort(arr);
int index = Arrays.binarySearch(arr, 2);
메소드에서 배열 return
public int[] createArray() {
return new int[]{1, 2, 3};
}
배열을 파라미터로 전달
public void printArray(int[] arr) {
for (int num : arr) {
System.out.println(num);
}
}
명령줄 인자
public class CommandLineArgs {
public static void main(String[] args) {
for (String arg : args) {
System.out.println(arg);
}
}
}
foreach 문에서 배열 사용
int[] numbers = {10, 20, 30};
for (int num : numbers) {
System.out.println(num);
}'개발 이론 > JAVA' 카테고리의 다른 글
| Collection (0) | 2025.02.02 |
|---|---|
| 상속 (0) | 2025.02.01 |
| 객체지향이란? (0) | 2025.02.01 |
| 메소드 (0) | 2025.01.31 |
| Statement와 Exception (0) | 2025.01.30 |