移除值后移动数组(Java)

  • 本文关键字:Java 数组 移动 java arrays
  • 更新时间 :
  • 英文 :


我制作了一个程序,它生成一个随机int数组,如果用户试图添加一个int,则大小会翻倍。例如:1|2|3|4如果他们要添加另一个int的话,它看起来就像1|2|3|4|5|0|0|0。我已经制作了一个添加int的方法,这个方法很有效,但现在我正在尝试制作删除某个int中的一个和删除所有int的另一个的方法。例如,removeInt(3)会给我1|2|0|4|5|0|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,为什么不简单地检查数字并向左移动(排序)?

最新更新