Converting an int array to a String array

前端 未结 14 1452
予麋鹿
予麋鹿 2020-12-01 14:21

So I have this \"list\" of ints. It could be a Vector, int[], List, whatever.

My goal though is to sort the

14条回答
  •  广开言路
    2020-12-01 14:51

    Java 8 way, no loops at all:

     // Given
    int[] array         = {-5, 8, 3, 10, 25};
    // When
    String[] actual = Arrays.stream(array)
            .sorted()
            .mapToObj(String::valueOf)
            .toArray(String[]::new);
    // Then
    String[] expected = {"-5", "3", "8", "10", "25"};
    
    assertArrayEquals(expected, actual);
    

提交回复
热议问题