java 문법 -5 배열 , ArrayList

Sukhun-Net·2023년 11월 5일
import java.util.ArrayList;

public class Main5{
	public static void main(String args[]){
		
		// 선언과 동시에 초기화 
		// 방법1: int[] score = new int[5] 
		// 방법2: 배열 값까지 부여 
		int[] score = {10, 20, 30, 40, 50};
		

		int count = score.length;
		System.out.println(count);
		
		System.out.println(score[score.length-1]);

		

		// String[] names = new String[2]; -> NULL 반환 
		// System.out.println(names[0].length()); -> 이와 같이 참조해서 무엇을 하려고 하면, NullPointerException 발생 
		
		String[] names = {"홍길동", "이순신"};
		
		System.out.println(names[0].length());
		
		
		
		
		//ArrayList 
		
		ArrayList<Integer> scoreList = new ArrayList<>();
		scoreList.add(10);
		scoreList.add(20);
		scoreList.add(30);
		scoreList.add(40);
		scoreList.add(50);
		
		System.out.println(scoreList.size());
		System.out.println(scoreList.get(0)); // 0번째 값 
		
		scoreList.add(2, 200); // 2번째 인덱스에 200 넣고 싶다. 
		scoreList.remove(2);
		System.out.println(scoreList); // 전체 모습 보기  
		
	}
	
	
}
profile
Data Scientist (Computer Vision, Multimodal)

0개의 댓글