java - 删除值后移动数组 (Java)

标签 java arrays

我编写了一个程序,该程序会生成一个随机整数数组,并且如果用户尝试添加整数,则大小会加倍。示例: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;
}

关于java - 删除值后移动数组 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28308965/

相关文章:

java - @ModelAttribute注解中声明的Bean的范围是什么

arrays - Valgrind错误-条件跳转或移动取决于未初始化的值

java - 递归java方法解析带条件的字符串?

java - JAXB 解码到具体类而不使用 xsi :type in xml but using actual concrete class name

java - Android Studio 错误 : class, 界面,或预期的枚举

c++ - 为什么 alloca 两次返回相同的地址?

javascript - JavaScript 中的 forEach 可以返回吗?

arrays - 整数数组的校验和?

Java Sql 从列中选择月份

Java 7 zip 文件系统提供程序似乎不接受 URI 中的空格