더 많이 실패하기

자바의정석 연습문제 풀이 6장 6-20 본문

자바/자바의정석-연습문제

자바의정석 연습문제 풀이 6장 6-20

김발자~ 2022. 9. 29. 20:42
반응형

자바의 정석 3판 연습문제 풀이

 

연습문제 파일은 아래 링크에서 다운받을 수 있다

https://github.com/castello/javajungsuk3/tree/master/%EC%97%B0%EC%8A%B5%EB%AC%B8%EC%A0%9C%ED%92%80%EC%9D%B4

 

GitHub - castello/javajungsuk3: soure codes and ppt files of javajungsuk 3rd edition

soure codes and ppt files of javajungsuk 3rd edition - GitHub - castello/javajungsuk3: soure codes and ppt files of javajungsuk 3rd edition

github.com

 

 

http://www.yes24.com/Product/Goods/24259565

 

Java의 정석 - YES24

최근 7년동안 자바 분야의 베스트 셀러 1위를 지켜온 `자바의 정석`의 최신판. 저자가 카페에서 12년간 직접 독자들에게 답변을 해오면서 초보자가 어려워하는 부분을 잘 파악하고 쓴 책. 뿐만 아

www.yes24.com

자바의 정석 저자 남궁성 님이 직접 올려주신 문제다

 


챕터6은 총 24문제로 구성되어 있다


6-20


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Excercise6_20 {
    public static int[] shuffle(int[] arr) {
        if(arr == null || arr.length == 0) {//★유효성 체크★
            return arr;                        //★유효성 체크★
        }
        
        for(int i = 0; i < arr.length; i++) {
            int j = (int) (Math.random() * arr.length);
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
        return arr;
    }
    public static void main(String[] args) {
        int[] original = {1,2,3,4,5,6,7,8,9};
        System.out.println(java.util.Arrays.toString(original));
        
        int[] result = shuffle(original);
        System.out.println(java.util.Arrays.toString(result));
    }
}
cs

 

유효성 체크(완전 낯설다)를 하지 않았다가 답지를 보고 추가했다

어떤 값이 들어올지 모르니 시작 전 유효성 체크를 하는 게 필수라고 한다

 

 


빈 변수 하나 만들고 서로 값을 바꾸는 걸 직접 쳐본 건 처음인 것 같다

반응형
Comments