day20_ArrayListLotto8

육희영·2021년 11월 1일
0

랜덤된 로또번호 뽑고 정렬하기

package com.java1.day20;

import java.util.*;
//ArrayList와 Collections를 이용하여 로또를 만들자!
//Math.random()를 쓰지 않고도 만들수 있다.
public class ArrayListLotto8 {

	public static void main(String[] args) {
		ArrayList list = new ArrayList();
		
		for(int i=1; i< 46; i++) {
			list.add(i);
		}
		System.out.println(list);
		
		Collections.shuffle(list);	// 리스트 요소의 순서를 섞는다.
		System.out.println(list);
		
		List list2 = list.subList(0, 6);// 리스트의 0번째 부터 5번째 까지의 요소를 가져온다.
		System.out.println(list2);
		
		Collections.sort(list2);	// 오름차순 정렬을 한다.	
		System.out.println(list2);
	}
}

출력결과

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45]
[17, 5, 31, 13, 40, 2, 44, 28, 38, 37, 7, 43, 21, 30, 22, 16, 41, 18, 10, 15, 1, 42, 19, 45, 20, 12, 33, 23, 35, 9, 25, 34, 11, 14, 24, 6, 4, 3, 36, 8, 32, 27, 39, 29, 26]
[17, 5, 31, 13, 40, 2]
[2, 5, 13, 17, 31, 40]

0개의 댓글

관련 채용 정보