Collections.shuffle

JWbase·2023년 2월 24일
0

Java

목록 보기
1/3

shuffle 메소드

  • Colletions에 있는 메소드 중 shuffle() 메소드는 배열이나 리스트를 랜덤으로 출력할 때 사용
public class ShuffleEx {
    public static void main(String[] args) {

        ArrayList<String> fruitList = new ArrayList<>();
        fruitList.add("apple");
        fruitList.add("banana");
        fruitList.add("grape");
        fruitList.add("melon");
        fruitList.add("orange");

        System.out.println("fruitList = " + fruitList);

        Collections.shuffle(fruitList);
        System.out.println("shuffleFruitList = " + fruitList);
    }
}
fruitlist = [apple, banana, grape, melon, orange]
shuffleFruitList = [melon, banana, orange, grape, apple]
profile
기억 저장소!!

0개의 댓글