"자바 Arrays.fill()"의 두 판 사이의 차이

34번째 줄: 34번째 줄:
     public static void main(String args[]) {
     public static void main(String args[]) {
         int[][] arr = new int[3][5];
         int[][] arr = new int[3][5];
         for(int[] row: arr) Arrays.fill(row, 10);
         for(int[] row: arr) {
            Arrays.fill(row, 10);
        }
         for(int[] row: arr) {
         for(int[] row: arr) {
             System.out.println( Arrays.toString(row) );
             System.out.println( Arrays.toString(row) );
            // [10, 10, 10, 10, 10]
            // [10, 10, 10, 10, 10]
            // [10, 10, 10, 10, 10]
         }
         }
        // [10, 10, 10, 10, 10]
        // [10, 10, 10, 10, 10]
        // [10, 10, 10, 10, 10]
    }
}
</source>
{{소스헤더|3차원 배열 채우기}}
<source lang='java'>
import java.util.Arrays;
public class MyClass {
    public static void main(String args[]) {
        int[][][] arr = new int[3][5][2];
        for(int[][] row2: arr) {
            for(int[] row: row2) {
                Arrays.fill(row, 10);
            }
        }
        for(int[][] row2: arr) {
            for(int[] row: row2) {
                System.out.print( Arrays.toString(row) );
            }
            System.out.println();
        }
        // [10, 10][10, 10][10, 10][10, 10][10, 10]
        // [10, 10][10, 10][10, 10][10, 10][10, 10]
        // [10, 10][10, 10][10, 10][10, 10][10, 10]
     }
     }
}
}

2019년 1월 12일 (토) 14:46 판

1 개요

Arrays.fill()
기본 채우기
import java.util.Arrays;
public class MyClass {
    public static void main(String args[]) {
        int[] arr = new int[] {1, 2, 3, 4, 5};
        Arrays.fill(arr, 100);
        System.out.println( Arrays.toString(arr) );
        // [100, 100, 100, 100, 100]
    }
}
시작, 끝지점 지정하여 채우기
import java.util.Arrays;
public class MyClass {
    public static void main(String args[]) {
        int[] arr = new int[] {1, 2, 3, 4, 5};
        Arrays.fill(arr, 2, 4, 100);
        System.out.println( Arrays.toString(arr) );
        // [1, 2, 100, 100, 5]
    }
}
2차원 배열(여러 행) 채우기
import java.util.Arrays;
public class MyClass {
    public static void main(String args[]) {
        int[][] arr = new int[3][5];
        for(int[] row: arr) {
            Arrays.fill(row, 10);
        }
        for(int[] row: arr) {
            System.out.println( Arrays.toString(row) );
        }
        // [10, 10, 10, 10, 10]
        // [10, 10, 10, 10, 10]
        // [10, 10, 10, 10, 10]
    }
}
3차원 배열 채우기
import java.util.Arrays;
public class MyClass {
    public static void main(String args[]) {
        int[][][] arr = new int[3][5][2];
        for(int[][] row2: arr) {
            for(int[] row: row2) {
                Arrays.fill(row, 10);
            }
        }
        for(int[][] row2: arr) {
            for(int[] row: row2) {
                System.out.print( Arrays.toString(row) );
            }
            System.out.println();
        }
        // [10, 10][10, 10][10, 10][10, 10][10, 10]
        // [10, 10][10, 10][10, 10][10, 10][10, 10]
        // [10, 10][10, 10][10, 10][10, 10][10, 10]
    }
}

2 같이 보기

3 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}