Java排序数组正升序到负升序

我无法解决问题,我需要数组 A 的输出,如数组中随机数的 {1,2,3,4,-1,-2,-3,-4},然后将其写入另一个数组 B . 到目前为止,我的实验代码并没有像我想的那样工作


public static void main(String[] args) {


    int a[] = {5,4,3,2,1,-3,-2,-30};

    int length = a.length - 1;


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

        for (int j = 0 ; j < length-i ; j++) {

            if (a[j] < a[j+1]) {

                int swap = a[j];

                a[j] = a[j+1];

                a[j+1] = swap;

            }

        }

    }


    for (int x : a) {

        System.out.print(x+" ");

    }

}

输出是 5 4 3 2 1 -2 -3 -30 ,但我需要 1,2,3,4,5,-2,-3,-30


更新:


public static void main(String[] args) {


    int a[] = {5,4,3,2,1,-3,-2,-30,-1,-15,8};

    int length = a.length - 1;


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

        for (int j = 0 ; j < length-i ; j++) {

            if (a[j] < a[j+1]) {

                int swap = a[j];

                a[j] = a[j+1];

                a[j+1] = swap;

            } else {

                if (a[j] > a[j+1] && a[j+1] > 0) {

                    int swap = a[j];

                    a[j] = a[j+1];

                    a[j+1] = swap;

                }

            }

        }

    }


    for (int x : a) {

        System.out.print(x+" ");

    }

}

我离目标更近了,但是 8 1 2 3 4 5 -1 -2 -3 -15 -30 ,那个数字 8 毁了这一切


PIPIONE
浏览 233回答 3
3回答

元芳怎么了

添加 if-else 以区分正负情况。if (a[j] < 0) {&nbsp; &nbsp; if (a[j] < a[j+1]) {&nbsp; &nbsp; &nbsp; &nbsp; int swap = a[j];&nbsp; &nbsp; &nbsp; &nbsp; a[j] = a[j+1];&nbsp; &nbsp; &nbsp; &nbsp; a[j+1] = swap;&nbsp; &nbsp; }} else {&nbsp; &nbsp; if (a[j] > a[j+1] && a[j+1] > 0) {&nbsp; &nbsp; &nbsp; &nbsp; int swap = a[j];&nbsp; &nbsp; &nbsp; &nbsp; a[j] = a[j+1];&nbsp; &nbsp; &nbsp; &nbsp; a[j+1] = swap;&nbsp; &nbsp; }}

慕妹3146593

我在这里使用了库函数。但是,如果您愿意,可以使用相同的想法编写函数。public class PostivieAsendingNegativeDesending implements Comparator<Integer> {&nbsp; &nbsp; &nbsp; &nbsp; public static void main(String args[]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int fullList[] = {5, 4, 3, 2, 1, -3, -2, -30};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Integer> subList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Integer> subList2 = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < fullList.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (fullList[i] < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subList2.add((fullList[i]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subList.add(fullList[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(subList);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(subList2, new PostivieAsendingNegativeDesending());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subList.addAll(subList2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < subList.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(subList.get(i)&nbsp; + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public int compare(Integer n1, Integer n2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return n2 - n1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

慕姐4208626

我建议另一种方法。您应该尝试制定精确比较必须遵守的规则。您的要求似乎有以下规则:正数总是在负数之前。正数按升序排列。负数按降序排列。是的,我说的是下降。由于较高的数字在较低的数字之前,即-2 大于-7。警告:您使用的是嵌套 for 循环,这意味着如果数组变大,处理时间将呈指数增长。好消息是:您不需要将 for 循环嵌套到另一个 for 循环中。我建议Comparator改为写一个:// The contract of Comparator's only method 'compare(i, j)' is that you// return a negative value if i < j, a positive (nonzero) value if i > j and// 0 if they are equal.final Comparator<Integer> c = (i, j) -> { // I'm using a lambda expression,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // see footnote&nbsp; &nbsp; // If i is positive and j is negative, then i must come first&nbsp; &nbsp; if (i >= 0 && j < 0) {&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; }&nbsp; &nbsp; // If i is negative and j is positive, then j must come first&nbsp; &nbsp; else if (i < 0 && j >= 0) {&nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; }&nbsp; &nbsp; // Else, we can just subtract i from j or j from i, depending of whether&nbsp; &nbsp; // i is negative or positive&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; return (i < 0 ? j - i : i - j);&nbsp; &nbsp; }}您的代码可能如下所示:int[] a = { 5, 4, 3, 2, 1, -3, -2, -30 };int[] yourSortedIntArray = Arrays.stream(a)&nbsp; &nbsp; .boxed()&nbsp; &nbsp; .sorted(c) // Your Comparator, could also added inline, like&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// .sorted((i, j) -> { ... })&nbsp; &nbsp; .mapToInt(i -> i)&nbsp; &nbsp; .toArray();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java