是否可以从对象数组中删除索引?

时间:2019-09-10 14:28:51

标签: java

我已经看到您可以使用ArrayLists来执行此操作,但是可以使用普通的对象数组来执行此操作吗?

deleting an object from an array [java]

在每个开始时,从杯子中给玩家3个随机骰子。

  

杯子总共有13个骰子。

当玩家从杯子中获得3个骰子时,杯子中只剩下10个骰子。我试图将值设置为null,但这仍然算作数组中的索引,因此长度不会改变。

// Removing the 3 dice which were picked form the cup of the current player, the diceToTakeOut is a String array with a length of 3 the newTurnCup is a Object array with a length of 13
public static Dice [] cup(String [] diceTotakeOut, Dice [] newTurnCup){

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

        if (diceTotakeOut.length > 0) {
            if (diceTotakeOut[i] == newTurnCup[i].getName()) {
                newTurnCup[i] = null;
            }
        }
    }

    return newTurnCup;
}

我尝试了几种可以想到的方式:

newTurnCup[i] = null;

newTurnCup[i] = newTurnCup[i - 1];

结果将是在每转一圈之后,持有骰子的数组将减少3个索引。

7 个答案:

答案 0 :(得分:2)

这是不可行的,因为数组的大小是固定的。

但是尝试使用ArrayList,它允许您使用方法remove(int index)动态删除元素

或:

将数组的内容从目标索引开始移到末尾。 (移位:从左到右)。

答案 1 :(得分:1)

您不能使用数组彻底做到这一点;您每次都必须重新创建一个较短的数组。

您可以使用其他数据结构,例如LinkedList

答案 2 :(得分:1)

您不能那样做。在Java中,数组是不可变的对象。创建后,无法更改此数组的大小(确保可以修改数组项)。

要使用各种大小的集合,您应该使用List集合:ArrayList-这是基于内部数组的附加属性-大小,LinkedList

List<Dice> dices = new ArrayList<>();   // dices.size() -> 0
dices.add(null);    // dices.size() -> 1    
dices.add(null);    // dices.size() -> 2
dices.remove(0);    // dices.size() -> 1

为清楚说明数组的不变性,这是一个如何从数组中手动​​删除项目的示例:

public static Dice[] removeItem(Dice[] dices, int i) {
    if (dices == null || dices.length == 0)
        return dices;
    if (i < 0 || i >= dices.length)
        throw new ArrayIndexOutOfBoundsException();

    Dice[] res = new Dice[dices.length - 1];
    System.arraycopy(dices, 0, res, 0, i);
    System.arraycopy(dices, i + 1, res, i, dices.length - i - 1);
    return res;
}

Dice[] arr = null;
arr = removeItem(arr, 5);

答案 3 :(得分:1)

由于您使用的是array而不是ArrayList,因此可以通过解决方法来实现。

public static Dice[] removeTheElement(Dice[] arr, int index) { 

    // If the array is empty 
    // or the index is not in array range 
    // return the original array 
    if (arr == null
        || index < 0
        || index >= arr.length) { 

        return arr; 
    } 

    // Create another array of size one less 
    Dice[] anotherArray = new Dice[arr.length - 1]; 

    // Copy the elements except the index 
    // from original array to the other array 
    for (int i = 0, k = 0; i < arr.length; i++) { 

        // if the index is 
        // the removal element index 
        if (i == index) { 
            continue; 
        } 

        // if the index is not 
        // the removal element index 
        anotherArray[k++] = arr[i]; 
    } 

    // return the resultant array 
    return anotherArray; 
} 

因此在这里,我们将正在使用的Object以及要删除的数组的索引作为参数传递。我们在函数内部创建一个临时Object,然后返回不包含具有我们提供的索引号的元素的

答案 4 :(得分:1)

Java中的数组变量是指向内存区域的指针,该内存区域与其容量和所包含类型的大小成正比。该区域不能动态修改。执行所需操作的唯一方法是创建一个新的数组变量,该变量已分配了所需的新数组大小。这也是实现此功能的Collections在后台发生的事情,只是存在优化措施,以便仅在需要时才执行此操作(可能非常昂贵)。

所以我最好的建议是将您的设计更改为使用列表,例如ArrayLists。链表虽然是一个不错的主意,但实际上通常避免使用链表,因为它们速度较慢(它们不利用硬件内存缓存)。

答案 5 :(得分:1)

如其他答案所述,您不能直接从数组中删除元素。您需要编写一个自定义方法,该方法将复制除需要删除的元素之外的所有元素。

但是,我建议您使用Apache Commons中的ArrayUtils。

ArrayUtils.remove(array, index)

文档中的一些示例:

ArrayUtils.remove(["a", "b"], 0)      = ["b"]
ArrayUtils.remove(["a", "b"], 1)      = ["a"]

引用:ArrayUtils Documentation

答案 6 :(得分:0)

您最好的选择是使用ArrayList,然后再操纵数组。

如果您想弄脏手并接受必须创建新的阵列,可以执行以下操作:

private static String[] removeIndex(String[] inputArray, int indexToRemove){
    if (indexToRemove >= inputArray.length) {
        throw new IndexOutOfBoundsException("index "  + indexToRemove + " is not allowed for array with size=" + inputArray.length);
    }
    String[] resultArray = new String[inputArray.length - 1 ];
    System.arraycopy(inputArray, 0, resultArray, 0, indexToRemove);
    System.arraycopy(inputArray, indexToRemove + 1, resultArray, indexToRemove, resultArray.length - indexToRemove);

    return resultArray;
}

然后食用它:

String[] inputArray = new String[]{"1","2","3","4","5","6","7","8","9","10"};

System.out.println(Arrays.toString(removeIndex(inputArray, 0)));
System.out.println(Arrays.toString(removeIndex(inputArray, 1)));
System.out.println(Arrays.toString(removeIndex(inputArray, 2)));
System.out.println(Arrays.toString(removeIndex(inputArray, 3)));
System.out.println(Arrays.toString(removeIndex(inputArray, 4)));
System.out.println(Arrays.toString(removeIndex(inputArray, 5)));
System.out.println(Arrays.toString(removeIndex(inputArray, 6)));
System.out.println(Arrays.toString(removeIndex(inputArray, 7)));
System.out.println(Arrays.toString(removeIndex(inputArray, 8)));
System.out.println(Arrays.toString(removeIndex(inputArray, 9)));
System.out.println(Arrays.toString(removeIndex(inputArray, 10)));

产生:

[2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Exception in thread "main" java.lang.IndexOutOfBoundsException: index 10 is not allowed for array with size=10
at Scratch.removeIndex(scratch.java:81)
at Scratch.main(scratch.java:73)