How to use java.util.Arrays

后端 未结 5 1523
挽巷
挽巷 2021-01-02 09:03

I\'m trying to use the java.util.Arrays class in JavaSE 6 but not sure how i would implement it? on an array that I have generated?

before the start of the class i

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 10:04

    Well let's say you have an array

    int[] myArray = new int[] { 3, 4, 6, 8, 2, 1, 9};
    

    And you want to sort it. You do this:

    // assumes you imported at the top
    Arrays.sort(myArray);
    

    Here's the whole shebang:

    import java.util.Arrays;
    class ArrayTest {
        public static void main(String[] args) {
            int[] myArray = new int[] { 3, 4, 6, 8, 2, 1, 9};
            Arrays.sort(myArray);
            System.out.println(Arrays.toString(myArray));
        }
    }
    

    And that results in

    C:\Documents and Settings\glow\My Documents>java ArrayTest
    [1, 2, 3, 4, 6, 8, 9]
    
    C:\Documents and Settings\glow\My Documents>
    

提交回复
热议问题