본문 바로가기
Java

[Java] List<Integer> To int[]

by jn4624 2022. 4. 26.
반응형

1. For문

public static void main(String[] args) {
    List<Integer> list = Arrays.asList(1,2,3,4,4,5,5,6);

    int[] arr = new int[list.size()];
    for( int i=0; i<list.size(); i++ ) {
        arr[i] = list.get(i);
    }

    System.out.println(Arrays.toString(arr));
}
public static void main(String[] args) {
    List<Integer> list = Arrays.asList(1,2,3,4,4,5,5,6);

    int[] arr = new int[list.size()];
    Iterator<Integer> iterator = list.iterator();

    for( int i=0; i<list.size(); i++ ) {
        arr[i] = iterator.next().intValue();
    }

    System.out.println(Arrays.toString(arr));
}

 

2. stream()

public static void main(String[] args) {
    List<Integer> list = Arrays.asList(1,2,3,4,4,5,5,6);

    int[] arr = list.stream().mapToInt(Integer::intValue).toArray();

    System.out.println(Arrays.toString(arr));
}
public static void main(String[] args) {
    List<Integer> list = Arrays.asList(1,2,3,4,4,5,5,6);

    int[] arr = list.stream().mapToInt(i -> i).toArray();

    System.out.println(Arrays.toString(arr));
}

 

 

🙏 참조 ::

반응형