Seung's Learning Record

[ JAVA ] 배열 본문

프로그래밍/JAVA

[ JAVA ] 배열

70_0ewd 2022. 8. 18. 00:48

배열을 선언할때  참조변수만 먼저 선언하여 크기 및 값을 이후에 초기화 하는 것도 가능하고 최초 선언시 부터 배열의 크기 및 값을 할당해 주는 것도 가능하다.

배열의 길이는 최초에 선언된 값으로 고정되며, 숫자 배열은 0으로, 문자열 배열은 null로, boolean 배열은 false로 값이 초기화 된다.

import java.io.*;
import java.util.Scanner;

public class ARRAY{
	public static void main(String[] args)throws IOExceptions{
    	Scanner sc = new Scanner(System.in);
        
        int a = sc.nextInt();			//배열 길이용 변수        
        int[] array1 = new int[a];		//선언과 길이 할당 동시에 하기         
        int[] array3;
        array3 = new int[a];			//선언 후 길이 할당 하기        
 
        int[] array2 = new int[]; 		//배열 길이 미설정시 오류
        
        for(int i=0; i<a ; i++)
        	array1[i] = sc.nextInt();	//배열에 값 입력
            
        //배열의 길이를 정확히 모르는 경우 배열명.length로 알수있음    
        for(int j=0 ; j<array1.length; j++)
        	System.out.println(array[j]);
            
        }
 }