N개씩 묶어서 for 루프

1 개요[ | ]

N개씩 묶어서 for 루프

2 Java[ | ]

import java.util.Arrays;
public class MyClass {
    public static void main(String args[]) {
        int[] arr = {1,2,3,4,5,6,7,8,9,10};
        int chunk_size = 4;
        for(int i=0; i<arr.length; i+=chunk_size) {
            System.out.println(Arrays.toString(Arrays.copyOfRange(arr, i, Math.min(arr.length,i+chunk_size))));
        }
    }
}

3 PHP[ | ]

$arr = [1,2,3,4,5,6,7,8,9,10];
$chunk_size = 3;
foreach( range(0, count($arr), $chunk_size) as $i ) {
    $chunk = array_slice($arr, $i, $chunk_size);
    echo json_encode($chunk) . "\n";
}

4 Python[ | ]

lst = [1,2,3,4,5,6,7,8,9,10]

chunk_size = 3
for i in range(0, len(lst), chunk_size):
    chunk = lst[i:i+chunk_size]
    print( chunk )

5 같이 보기[ | ]

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