数组和集合 / 数组

import java.util.*;

int[] numbers = {235711};
int[] first2 = Arrays.copyOf(numbers, 2); // [ 2, 3 ]

int[] last3 = Arrays
    .copyOfRange(numbers, 2, numbers.length); // [ 5, 7, 11 ]

// Note: Stream API is slower than direct array copying due to
// stream overhead and potential boxing/unboxing costs.
var first3 = Arrays.stream(numbers)
    .limit(3)
    .toArray(); // [ 2, 3, 5 ]
var last4 = Arrays.stream(numbers)
    .skip(Math.max(0, numbers.length - 4))
    .toArray(); // [ 3, 5, 7, 11 ]

System.out.println(Arrays.toString(first2));
System.out.println(Arrays.toString(last3));
System.out.println(Arrays.toString(first3));
System.out.println(Arrays.toString(last4));