TIL - 3월 14일 : 배열

박경서·2024년 3월 19일

기본형 배열

  • 선언 여러가지 가능
ex1. 기본형타입[] 변수명;
ex2. 기본형타입 변수명[];
  • 초기값 가지고 선언
기본형타입[] 변수명 = new 기본형타입[배열의크기];
변수명[index값] =;
기본형타입[] 변수명 = new 기본형타입[]{1,2, .... };
기본형타입[] 변수명 = {1,2,3.... };

참조형 배열

public class Array05 {
	public static void main(String[] args) {
		ItemForArray[] array1 = new ItemForArray[3];
		array1[0] = new ItemForArray(500, "사과");
		array1[1] = new ItemForArray(300, "바나나");
		array1[2] = new ItemForArray(900, "수박");
		
		ItemForArray[] array2 = new ItemForArray[]{new ItemForArray(500, "사과"), new ItemForArray(300, "바나나"), new ItemForArray(900, "수박")};
		ItemForArray[] array3 = {new ItemForArray(500, "사과"), new ItemForArray(300, "바나나"), new ItemForArray(900, "수박")};
		
		System.out.println(array1[0].getName());
		System.out.println(array1[0].getPrice());
		System.out.println(array1[1].getName());
			System.out.println(array1[1].getPrice());
		System.out.println(array1[2].getName());
		System.out.println(array1[2].getPrice());
	}
}

이차열 배열

  • 리스트
	타입[][] 변수명 = new 타입[행의수][열의수];
	변수명[행인덱스][열인덱스] =;
public class Array09 {
	public static void main(String[] args) {
		int[][] array = {{0,1,2}, {3,4,5}};
		
		for(int i = 0; i < array.length; i++){
			for(int j = 0; j < array[i].length; j++){
				System.out.print(array[i][j] + "\t");
			}
			System.out.println();
		}
	}
}

이차원 가변 배열의 선언과 초기화

  • 가변 길이 : 두 개의 길이가 달라도 된다는 것을 의미
	타입[][] 변수명 = new 타입[행의수][];
	변수명[행의인덱스] = new 타입[열의수];
public class Array10 {
    public static void main(String[] args) {
        int[][] array = new int[2][];
        
        array[0] = new int[2];
        array[1] = new int[3];
        
        array[0][0] = 0;
        array[0][1] = 1;
        array[1][0] = 2;
        array[1][1] = 3;
        array[1][2] = 4;
        
        for(int i = 0; i < array.length; i++){
            for(int j = 0; j < array[i].length; j++){
                System.out.print(array[i][j] + "\t");
            }
            System.out.println();
        }
    }
}
  • for each 문
public class Array12 {
	public static void main(String[] args) {
		
		int[] array = {1,2,3,4,5};
		
		for(int i : array){
			System.out.println(i);
		}
	}
}

// i는 index가 아닌 array안의 값

명령 행 아규먼트

  • 리스트
public class EmptyCommandLineArgumentExam {
	public static void main(String[] args){
		System.out.println(args.length);
	}
}

제한 없는 아규먼트

  • 리스트
public class UnlimitiedArgumentsExam {
	public static void main(String[] args){
		
		System.out.println(sum(5,10));
		System.out.println(sum(1,2,4,2));
		System.out.println(sum(3,1,2,3,4,1));
	}

	public static int sum(int... args){
		System.out.println("print1 메소드 - args 길이 : " + args.length);
		int sum = 0;
		for(int i = 0; i < args.length; i++){
			sum += args[i];
		}
		return sum;
	}
}

//print1 메소드 - args 길이 : 2
//15
//print1 메소드 - args 길이 : 4
//9
//print1 메소드 - args 길이 : 6
//14

생성자

  • 객체가 생성될때 자동으로 호출 되는 특수 목적의 메서드
  • 객체의 초기화를 위해 사용
public class Book {
	String title;
	int price;
		
	public Book() {	}                   // 기본생성자
	
	public Book(String title, int price) {    // 매개변수를 가진 생성자
		this.title = title;
		this.price = price;
	}
}
  • 생성자(Constructor)는 객체가 생성될때 자동으로 호출되는 특수 목적의 멤버함수(메소드)로객체의 초기화를 위해 사용
profile
안녕하세요, 박경서입니다.

0개의 댓글