删除值后移动数组(Java)

删除值后移动数组(Java),java,arrays,Java,Arrays,我制作了一个程序,它可以生成一个随机整数数组,如果用户试图添加一个整数,其大小会加倍。例如:1 | 2 | 3 | 4如果他们要添加另一个整数,则看起来像1 | 2 | 3 | 4 | 5 | 0 | 0。我已经创建了一个方法来添加一个int,它可以工作,但是现在我正在尝试创建一个方法来删除一个特定int,另一个方法删除所有特定int。例如,removeInt3会给我1 | 2 | 0 | 4 | 5 | 0 | 0。我让第一部分工作,这样它就可以像这样将零移到末尾1 | 2 | 4 | 5 |

我制作了一个程序,它可以生成一个随机整数数组,如果用户试图添加一个整数,其大小会加倍。例如:1 | 2 | 3 | 4如果他们要添加另一个整数,则看起来像1 | 2 | 3 | 4 | 5 | 0 | 0。我已经创建了一个方法来添加一个int,它可以工作,但是现在我正在尝试创建一个方法来删除一个特定int,另一个方法删除所有特定int。例如,removeInt3会给我1 | 2 | 0 | 4 | 5 | 0 | 0。我让第一部分工作,这样它就可以像这样将零移到末尾1 | 2 | 4 | 5 | 0 | 0 | 0 | 0,但不能让它为多个相同的值工作。有什么建议吗

    // ****************************************************************
// IntegerList.java
//
// Define an IntegerList class with methods to create & fill
// a list of integers.
//
// ****************************************************************

public class IntegerList
{
    int[] list; //values in the list
//-------------------------------------------------------
//create a list of the given size
//-------------------------------------------------------
    public IntegerList(int size) 

    {
        list = new int[size];
    }
//-------------------------------------------------------
//fill array with integers between 1 and 100, inclusive
//-------------------------------------------------------
    public void randomize()
    {
        for (int i=0; i<list.length; i++)
            list[i] = (int)(Math.random() * 100) + 1;
    }
//-------------------------------------------==----------
//print array elements with indices
//-------------------------------------------------------
    public void print()
    {
        for (int i=0; i<list.length; i++)
            System.out.println(i + ":\t" + list[i]);
    }
    public void addElement(int newVal){
        boolean full = true;
        System.out.println(list.length);
        int position = 0;
        int place;
        while(position < list.length){
            System.out.println("HERE");
                if(list[position]==0){
                    System.out.println("here");
                    full = false;
                    place = position;
                    System.out.println(place);
                }
                position = position+1;
            }
        if(full == true){
            list = increaseSize(list);
            System.out.println("L"+list.length);
            full = false;
            }

        for(int i = 0;i<list.length;i++){
            if(list[i]==0){
                if(i<position){
                    position = i;
            System.out.println(list.length);
                }
            }
        }
        list[position] = newVal;
    }
    public void removeFirst(int newVal){
        int position = 0;
        boolean removed = false;
        for(int i = 0; i<list.length;i++){
            if(list[i] == newVal){
                list[i]=0;
                position = i;
                removed = true;
                break;
            }
        }
        if(removed==true){
            for(int i = position;i<list.length;i++){
                if(i!=list.length-1){
                    list[i]=list[i+1];
                }
            }
            list[list.length-1]= 0;
        }
    }
    public void removeAll(int newVal){
        int position = 0;
        boolean removed = false;
        for(int i = 0; i<list.length;i++){
            if(list[i] == newVal){
                list[i]=0;
                position = i;
                removed = true;
            }
        }
        if(removed==true){
            for(int i = 0;i<list.length;i++){
                if(i!=list.length-1 && list[i+1]==newVal){
                    list[i]=0;
                }
                if(list[i]==newVal){
                    list[i]=0;
                }
            }
        }
        }
    public static int[] increaseSize(int[] x){
        int newLength = x.length *2;
        int[] newx = new int[newLength];
        for(int i = 0; i<x.length; i++){
            newx[i] = x[i];
        }
        return newx;
    }
    public static int[] halfSize(int[] x){
        int[] newx = new int[x.length / 2];
        for(int i = 0; i<x.length; i++){
            newx[i] = x[i];
        }
        return newx;
    }
}

我相信有一种更容易实现removeAll方法的方法。在你的数组中移动2个而不是1个索引,不断地在你要移除的项目上移动值

int dest = 0;
int source = 0;
while (source < array.length) {
    if (array[dest] != valueToRemove)
        dest++;
    array[dest] = array[source++];
}
while (dest < array.length) {
    array[dest++] = 0;
}

我执行了你的代码,发现问题就在这一部分,在removeAll下

if(removed){
        for(int i = 0;i<list.length;i++){
            if(i!=list.length-1 && list[i+1]==newVal){
                list[i]=0;
            }
            if(list[i]==newVal){
                list[i]=0;
            }
        }
    }

如果您注释掉并重试一次,您将看到removeAll正在工作,您所需的数字将被0替换。现在,如果左边的数字大于0,为什么不简单地检查数字和移位排序?

如果“removeFirst”方法有效,您就不能继续调用“removeAll”方法吗?也许removeFirst返回一个布尔值,表示它是否找到要删除的元素,所以当它返回false时,您知道已经完成了吗?