Java-对数组进行排序

Java-对数组进行排序,java,arrays,Java,Arrays,我在整理数组时遇到了一些轻微而愚蠢的问题。 下面是我正在做的和我想做的: 我生成一个整数数组(good ol't=new int[];),其中填充了从-1到9的随机生成的数字。数组的大小无关。我想让每一个“-1”都到达数组的末尾,而每一个“-1”都被“推”到该位置 因为我不是以英语为母语的人,所以很难在这个问题上正确表达自己 例如: 我的阵型是:92-146-1 我要:92461-1 我目前设法得到这个,但它不会工作,如果有两个连续-1原因,第二个将被交换 例: 首字母:92-1-14 6 莫迪

我在整理数组时遇到了一些轻微而愚蠢的问题。 下面是我正在做的和我想做的:

我生成一个整数数组(good ol't=new int[];),其中填充了从-1到9的随机生成的数字。数组的大小无关。我想让每一个“-1”都到达数组的末尾,而每一个“-1”都被“推”到该位置

因为我不是以英语为母语的人,所以很难在这个问题上正确表达自己

例如: 我的阵型是:92-146-1 我要:92461-1

我目前设法得到这个,但它不会工作,如果有两个连续-1原因,第二个将被交换

例: 首字母:92-1-14 6 莫迪夫之后:92-146-1

正如我所说的,这个问题很可能就要解决了,但我现在真的找不到解决办法

这是我的密码:

    for(int i=0;i<NB_CARD;i++)
    {
        t[i]=randomGenerator.nextInt(10)-1;
        System.out.print("t["+i+"]= "+t[i]+"\t\t");
        if(i==4){System.out.println("");}
    }

    //each -1 should be at the end of array
    for(int i=0;i<NB_CARD;i++)
    {
        int tmp=0;

        if(t[i]==-1) 
        {
            for(int y=i;y<NB_CARD-1;y++)
            {
                    tmp=t[y+1];
                    t[y+1]=t[y];
                    t[y]=tmp;
            }
        }
    }

    for(int i=0;i<NB_CARD;i++)
    {
        System.out.print("t["+i+"]= "+t[i]+"\t\t");
        if(i==4){System.out.println("");}
    }

for(int i=0;i您可以将数组转换为list,遍历list并删除所有-1,计算删除的数量。然后在列表末尾添加这些数量。如果最终结果必须是数组,则可以使用list.toArray方法将其转换回

public static void main(String[] args) {
    Integer[] t = {3, -1, -1, -1, 4, 5, 6, -1, -1};

    List<Integer> list = new ArrayList<>();
    Collections.addAll(list, t);

    int numberOfMinusOnes = 0;
    for (Integer number : list) {
        if (number == -1) {
            numberOfMinusOnes++;
        }
    }

    list.removeAll(Arrays.asList(-1));

    for (int i = 0; i < numberOfMinusOnes; i++) {
        list.add(-1);
    }

    list.toArray(t);

    System.out.println(Arrays.toString(t));
}
publicstaticvoidmain(字符串[]args){
整数[]t={3,-1,-1,4,5,6,-1,-1};
列表=新的ArrayList();
集合。addAll(列表,t);
int numberOfMinusOnes=0;
用于(整数:列表){
如果(数字==-1){
numberofminusone++;
}
}
removeAll(Arrays.asList(-1));
for(int i=0;i

使用google collections库可以简化一些boiler plate代码。我还建议将一些部分提取到单独的方法中,以提高可读性。

您可以将数组转换为列表,遍历列表并删除所有-1,计算删除的数量。然后在列表末尾添加这些部分。如果最终结果必须更改,请如果是数组,可以使用List.toArray方法将其转换回数组

public static void main(String[] args) {
    Integer[] t = {3, -1, -1, -1, 4, 5, 6, -1, -1};

    List<Integer> list = new ArrayList<>();
    Collections.addAll(list, t);

    int numberOfMinusOnes = 0;
    for (Integer number : list) {
        if (number == -1) {
            numberOfMinusOnes++;
        }
    }

    list.removeAll(Arrays.asList(-1));

    for (int i = 0; i < numberOfMinusOnes; i++) {
        list.add(-1);
    }

    list.toArray(t);

    System.out.println(Arrays.toString(t));
}
publicstaticvoidmain(字符串[]args){
整数[]t={3,-1,-1,4,5,6,-1,-1};
列表=新的ArrayList();
集合。addAll(列表,t);
int numberOfMinusOnes=0;
用于(整数:列表){
如果(数字==-1){
numberofminusone++;
}
}
removeAll(Arrays.asList(-1));
for(int i=0;i

使用google collections library可以简化一些boiler plate代码。我还建议将一些部分提取到单独的方法中,以提高可读性。

如果我正确理解代码,如果您找到
-1
,您最终会将其冒泡到最后

您可以做的是存储当前最后一个元素的位置,它在开始时是
n-1

当您找到一个
-1
时,您可以用当前的最后一个元素替换
i
th元素,然后将current的值减少1。这应该允许您始终将遇到的任何新
-1
元素放在末尾

代码方面:

    int[] t = new int[5];
    t[0] = -1; t[1] = -1; t[2] = 4;

    t[3] = -1;  //Just for demonstration purposes
    t[4] = -1;  //Just for demonstration purposes

    int currentLast = 0;

    for(int i = t.length - 1; i >=0; i--)
    {
        if(t[i] == -1)            
            currentLast = i - 1;
        else
            break;
    }

    for(int i = 0; i < t.length; i++)
        System.out.println(t[i]);

    System.out.println("After");


    for(int i = 0; i < currentLast; i++)
    {
        if(t[i] == -1)
        {   
            int temp = t[currentLast];
            t[currentLast] = t[i];
            t[i] = temp;
            currentLast--;
        }
    }

     for(int i = 0; i < t.length; i++)
        System.out.println(t[i]);
}

如果我正确理解了代码,如果你找到了
-1
,你最终会把它泡到最后

您可以做的是存储当前最后一个元素的位置,它在开始时是
n-1

当您找到一个
-1
时,您可以用当前的最后一个元素替换
i
th元素,然后将current的值减少1。这应该允许您始终将遇到的任何新
-1
元素放在末尾

代码方面:

    int[] t = new int[5];
    t[0] = -1; t[1] = -1; t[2] = 4;

    t[3] = -1;  //Just for demonstration purposes
    t[4] = -1;  //Just for demonstration purposes

    int currentLast = 0;

    for(int i = t.length - 1; i >=0; i--)
    {
        if(t[i] == -1)            
            currentLast = i - 1;
        else
            break;
    }

    for(int i = 0; i < t.length; i++)
        System.out.println(t[i]);

    System.out.println("After");


    for(int i = 0; i < currentLast; i++)
    {
        if(t[i] == -1)
        {   
            int temp = t[currentLast];
            t[currentLast] = t[i];
            t[i] = temp;
            currentLast--;
        }
    }

     for(int i = 0; i < t.length; i++)
        System.out.println(t[i]);
}

使用一个循环执行此操作的更有效方法是

int j = 0;
// copy all the non -1 values down.
for (int i = 0; i < NB_CARD; i++)
    if (t[i] != -1)
       t[j++] = t[i];
// fill the rest with -1
Arrays.fill(t, j, NB_CARD, -1);
印刷品

[3, 4, 5, 6, -1, -1, -1, -1, -1]
在Java8中,您可以这样做

int[] t = {6, -1, -1, -1, 4, 5, 3, -1, -1};
List<Integer> sorted = IntStream.of(t).boxed()
        .sorted((a, b) -> (a > -1 ? 1 : 0) - (b > -1 ? 1 : 0))
        .collect(Collectors.toList());
for (int i = 0; i < t.length; i++)
    t[i] = sorted.get(i);
System.out.println(Arrays.toString(t));

虽然它是O(N log N)而不是O(N),并且有更多的代码。

使用一个循环更有效的方法是

int j = 0;
// copy all the non -1 values down.
for (int i = 0; i < NB_CARD; i++)
    if (t[i] != -1)
       t[j++] = t[i];
// fill the rest with -1
Arrays.fill(t, j, NB_CARD, -1);
印刷品

[3, 4, 5, 6, -1, -1, -1, -1, -1]
在Java8中,您可以这样做

int[] t = {6, -1, -1, -1, 4, 5, 3, -1, -1};
List<Integer> sorted = IntStream.of(t).boxed()
        .sorted((a, b) -> (a > -1 ? 1 : 0) - (b > -1 ? 1 : 0))
        .collect(Collectors.toList());
for (int i = 0; i < t.length; i++)
    t[i] = sorted.get(i);
System.out.println(Arrays.toString(t));

虽然它是O(N log N)而不是O(N),并且有更多的代码。

我会使用,提供您自己的比较器,将-1放在输出的末尾。

我会使用,提供您自己的比较器,将-1放在输出的末尾。

使用冒泡排序

package com.appkart.collections;

import java.util.Random;

public class Test {

    public void fillRandomNumber(int a[]) {
        Random random = new Random();
        for (int i = 0; i < a.length; i++) {
            a[i] = random.nextInt(10)-1;
        }
    }

    public void printNumber(int a[]) {
        for (int i = 0 ; i < a.length ;i++) {
            System.out.print(a[i] +" ");
        }
    }

    //Using bubble sort
    public void sortRandomNumber(int a[]) {
        int length = a.length;
        int temp;

        for (int i = 0; i < length; i++) {
            for (int j = 1; j < length - i; j++) {
                if (a[j - 1] < a[j]) {
                    temp = a[j - 1];
                    a[j - 1] = a[j];
                    a[j] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {

        int a[] = new int[10];
        Test test = new Test();
        test.fillRandomNumber(a);
        System.out.println("Random Number bofore sort");
        test.printNumber(a); 
        test.sortRandomNumber(a);
        System.out.println("\n Random Number after sort");
        test.printNumber(a); 
    }
}
package com.appkart.collections;
导入java.util.Random;
公开课考试{
公共编号(int a[]){
随机=新随机();
for(int i=0;i
使用冒泡排序

package com.appkart.collections;

import java.util.Random;

public class Test {

    public void fillRandomNumber(int a[]) {
        Random random = new Random();
        for (int i = 0; i < a.length; i++) {
            a[i] = random.nextInt(10)-1;
        }
    }

    public void printNumber(int a[]) {
        for (int i = 0 ; i < a.length ;i++) {
            System.out.print(a[i] +" ");
        }
    }

    //Using bubble sort
    public void sortRandomNumber(int a[]) {
        int length = a.length;
        int temp;

        for (int i = 0; i < length; i++) {
            for (int j = 1; j < length - i; j++) {
                if (a[j - 1] < a[j]) {
                    temp = a[j - 1];
                    a[j - 1] = a[j];
                    a[j] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {

        int a[] = new int[10];
        Test test = new Test();
        test.fillRandomNumber(a);
        System.out.println("Random Number bofore sort");
        test.printNumber(a); 
        test.sortRandomNumber(a);
        System.out.println("\n Random Number after sort");
        test.printNumber(a); 
    }
}
package com.appkart.collections;
导入java.util.Random;
公开课考试{
公共编号(int a[]){
随机=新随机();
for(int i=0;i