HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🤩
개발
/
Java
Java
/
💯
Java 기본
/기본개념/
🎃
배열(Primitive type Array = Object)
🎃

배열(Primitive type Array = Object)

배열

배열 초기화하기

int[] numbers = new int[] {10, 20, 30}; //개수 생략해야 함 int[] numbers = {10, 20, 30}; // new int[] 생략 가능 int[] ids; ids = new int[] {10, 20, 30}; // 선언후 배열을 생성하는 경우는 new int[] 생략할 수 없음

객체 배열

  • 기본 자료형 배열은 선언과 동시에 배열의 크기 만큼의 메모리가 할당되지만, 객체 배열의 경우엔 요소가 되는 객체의 주소가 들어갈(4바이트, 8바이트) 메모리만 할당되고(null) 각 요소 객체는 생성하여 저장해야 함
  • 객체에 문자열이 멤버 변수로 되어있을 때, 걔의 길이도 사이즈 측정이 불가능하니 미리 메모리 할당이 불가능함

객체 복사

  • System.arraycopy - 얕은복사
  • 깊은 복사를 위해서는 element마다 일일히 생성해주어야함
 

Primitive Array ↔ Collection

  • Collection → Array는 매우 쉬움. method존재함
  • Array → Collection으로 생성 시, stream 활용!
int[] input = new int[]{1,2,3,4}; List<Integer> output = Arrays.stream(input).boxed().collect(Collectors.toList());