Sad Puppy 3 배열 :: 개발자 아지트

기본형(Primitive Type) : 변수에 사용할 값을 직접 넣을 수 있는 데이터 타입

참조형(Reference Type) : 데이터에 접근하기 위한 참조(주소)를 저장하는 데이터 타입

 

배열과 객체, 클래스를 담는 변수 등은 참조형에 속함.

 

 

배열은 동적으로 사이즈를 변경할 수 있다. 

코드가 실행되는 시점에 배열의 크기가 정해지기 때문이다. 

예를 들면, 사용자 입력을 받아 배열의 크기를 설정할 수 있다. 

 

기본 형태

	public class Array1{
    	public static void main(String[] args){
        	int[] team; // 배열 변수 선언
            teams = new int[4]; // 배열 생성
        }
    
    }

 

	public class Array1{
    	public static void main(String[] args){
        	int[] team; // 배열 변수 선언
            teams = new int[]{1, 2, 3, 4}; // 배열 생성
        }
    
    }

 

	public class Array1{
    	public static void main(String[] args){
        	int[] team = new int[]{1, 2, 3, 4}; // 배열 생성
        }
    
    }

 

 

	public class Array1{
    	public static void main(String[] args){
        	int[] team = {1, 2, 3, 4}; // 배열 생성
        }
    
    }

 

리펙토링 :

기존 코드의 기능은 유지하면서 내부 구조를 개선하는 것을 말함.

(중복 제거, 복잡성을 줄이기, 이해하기 쉬운 코드로 다시 작성)

 

 

2차원 배열

 

arr[행][열] 

 

	public class Array1{
    	public static void main(String[] args){
        	int[][] team = new int[2][3]; // 배열 생성
        }
    
    }

 

 

 

	public class Array1{
    	public static void main(String[] args){
        	int[][] team = new int[2][3]{
                {1, 2, 3},
                {4, 5, 6}    
            }; // 배열 생성
        }
    }

 

 

향상된 for 문

코드가 간결하고 가독성이 좋음

public class test{
	public static void main(String[] args){
    int[] numbers = {1, 2, 3, 4, 5};
    
 	for (int number : numbers){
    	System.out.println(number);
    }
    }

}

 

(단축키 - iter) 

'[프로그래머스]Java > Java입문' 카테고리의 다른 글

메서드  (0) 2024.07.05
사용자 입력  (1) 2024.07.05
스코프(변수의 접근 가능 범위), 형변환  (0) 2024.07.05
반복문  (0) 2024.07.05
조건문  (0) 2024.07.05

+ Recent posts