본문 바로가기

JAVA/CollectionFramework

List

MainWrapper

package ex02_List;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class MainWrapper {

	
	public static void ex01()
	{
		
		List<String>season;
		
		season=new ArrayList<String>();
		
		season.add("여름");
		season.add("가을");
		season.add("겨울");
		season.add(0,"봄");//위치, 요소
		
		for(int i=0,length=season.size();i<length;i++)
		{
		System.out.println(season.get(i));
		}
		
	}
	

	public static void ex02() {
		
		List<String> hobbies =new ArrayList<String>();
		
		hobbies.add("여행");
		hobbies.add("코딩");
		hobbies.add("독서");
		
		
		System.out.println(hobbies.size());
		System.out.println("----");
		System.out.println(hobbies.get(hobbies.size()-1));
		
		System.out.println("----");
		for(int i=0,length=hobbies.size();i<length;i++)
		{
			System.out.println(hobbies.get(i));
		}
		
	}
	public static void ex03() {
		
		List<String>flower=new ArrayList<String>();
		Scanner scan=new Scanner(System.in);
		
		for(int i=1;i<4;i++)
		{
		flower.add(scan.next());
		}
		
		System.out.println("↓");
		
		flower.set(0, "다투라");
		
		
		
		//조회
	  for(int i=0,length=flower.size();i<length;i++) {
	     System.out.println(flower.get(i));
	   }
	  
	 
		flower.remove(1);
		 System.out.println("↓");
		
		//조회
	  for(int i=0,length=flower.size();i<length;i++) {
	     System.out.println(flower.get(i));
	   }
	  }
	
	public static void ex04() {
		
		Integer[] a= {10,20,30,40,50};
		//배열을 ArrayList로 바꿈
		List<Integer>numbers=Arrays.asList(a);
		//초기화된  ArrayList는 길이변경불가
		
	  for(int i=0,length=numbers.size();i<length;i++)
    {
      System.out.println(numbers.get(i));
    }

	}
	
  public static void ex05() {
  
    
    //ArrayList 초기화
    List<Integer>numbers=Arrays.asList(10,20,30,40,50);
    //for문
    for(int i=0,length=numbers.size();i<length;i++)
    {
      System.out.println(numbers.get(i));
    }
}
	
	
	public static void main(String[] args) {

		ex01();
		System.out.println("----");
		ex02();
		System.out.println("----");
		ex03();
		System.out.println("----ex05결과와 비교하기");
		ex04();
		System.out.println("----");
		ex05();
	}

scanner 입력

장미

다투라

능소화

 

출력결과

 

벨로그주소:https://velog.io/@000000hj

깃허브 주소

https://github.com/000000hj/MyPractice/tree/main/11_CollectionFramework

 

'JAVA > CollectionFramework' 카테고리의 다른 글

generic 예제  (0) 2023.07.29