Java将二维数组的行排序为升序,将列排序为降序

拜托,有人可以帮助我,我需要按降序对二维数组列进行排序吗?我使用此代码将数组行按升序排序,但现在必须将列按降序排序。


// Initialize array

static int[][] sortArray = new int[7][7];


public static void main(String args[]) {

    //initialize array values with random numbers

    for (int i = 0; i < sortArray.length; i++) {

        for (int j = 0; j < sortArray[i].length; j++) {

            sortArray[i][j] = (int) (Math.random() * 100);


        }

    }


    System.out.println("\n" + "Before sorting");

    displayArray();


    //Print out sorted array

    for (int i = 0; i < sortArray.length; i++) {

        bubbleSort(sortArray[i]);

    }


    System.out.println("\n" + "Array rows in ascending order");

    displayArray();


    //Print out sorted columns of array

    for (int i = 0; i < sortArray.length; i++) {

        sortSort(sortArray[i]);

    }


    System.out.println("\n" + "Array column in descending order");

    displayArray();


}


// Sort rows into ascending order

public static void bubbleSort(int[] numArray) {

    int n = numArray.length;

    int temp = 0;


    for (int i = 0; i < n; i++) {

        for (int j = 1; j < (n - i); j++) {

            if (numArray[j - 1] > numArray[j]) {

                temp = numArray[j - 1];

                numArray[j - 1] = numArray[j];

                numArray[j] = temp;

            }

        }

    }

}


//Sort cols into descending order

public static void sortSort(int[] colArray) {

    int n = colArray.length;

    int temp = 0;


    for (int i = 0; i < n; i++) {

        for (int j = 1; j < (n - i); j++) {

            if (colArray[j - 1] < colArray[j]) {

                temp = colArray[j - 1];

                colArray[j - 1] = colArray[j];

                colArray[j] = temp;

            }

        }

    }

}



catspeake
浏览 359回答 3
3回答

墨色风雨

int[] colArray = new int[] { 2, 9, 4, 5};int n = colArray.length;int temp = 0;for (int i = 0; i < n; i++) {&nbsp; for (int j = 1; j < (n - i); j++) {&nbsp; &nbsp; &nbsp; if (colArray[j - 1] > colArray[j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = colArray[j - 1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colArray[j - 1] = colArray[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colArray[j] = temp;&nbsp; &nbsp; &nbsp; }&nbsp; }&nbsp;}&nbsp;Arrays.stream(colArray).forEach(data -> System.out.println(data));

开满天机

只需更换if (colArray[j - 1] < colArray[j]) {...}和:if (colArray[j - 1] > colArray[j]) {...}> 代替 <

不负相思意

首先,你的数组不是 2D :),只是因为数组的每个元素都有一个像 Arr[0] 这样的数字被调用,并且它的值是例如 5 它并不意味着它的 2D。但是为了将数组按降序排序,您可以使用以下代码:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int n = intArray.length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int temp = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i < n; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j=1; j < (n-i); j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(intArray[j-1] < intArray[j]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //swap the elements!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = intArray[j-1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intArray[j-1] = intArray[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intArray[j] = temp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java