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

Java 是否可以从对象数组中删除索引?,java,Java,我看到您可以使用ArrayLists来执行此操作,但是否可以使用普通对象数组来执行此操作 在每个游戏开始时,玩家从一个杯子中随机得到3个骰子 这个杯子总共有13个骰子 当玩家从杯子中得到3个骰子时,杯子应该只剩下10个骰子。我试图将该值设置为null,但这仍然作为数组中的索引,因此长度不会改变 // Removing the 3 dice which were picked form the cup of the current player, the diceToTakeOut is a S

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

在每个游戏开始时,玩家从一个杯子中随机得到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;
}
//除去从当前玩家的杯子中拾取的3个骰子,diceToTakeOut是一个长度为3的字符串数组newTurnCup是一个长度为13的对象数组
公共静态骰子[]杯(字符串[]骰子ToTakeout,骰子[]新TurnCup){
for(int i=0;i0){
if(diceTotakeOut[i]==newTurnCup[i].getName()){
newTurnCup[i]=null;
}
}
}
返回纽顿杯;
}
我尝试了几种我能想到的不同方法:

newTurnCup[i]=null

newTurnCup[i]=newTurnCup[i-1]


结果是,每回合后,持有骰子的阵法将少3个索引。

你不能用阵法干净利落地做到这一点;每次都必须重新创建一个较短的数组


您可以使用其他数据结构,例如
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
但是尝试使用
ArrayList
,它允许您使用
remove(int-index)

或:


将数组的内容从目标索引开始移动到结尾。(shift:从左向右)。

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

要拥有不同大小的集合,应使用
List
collection:
ArrayList
-这是基于具有附加属性-size、
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
List dices=new ArrayList();//骰子大小()->0
骰子。添加(空);//骰子大小()->1
骰子。添加(空);//骰子大小()->2
骰子。移除(0);//骰子大小()->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);
publicstaticdice[]removietem(Dice[]dices,inti){
if(dices==null | | dices.length==0)
返回骰子;
如果(i<0 | | i>=骰子长度)
将新的ArrayIndexOutOfBoundsException()抛出;
骰子[]res=新骰子[骰子长度-1];
数组复制(骰子,0,res,0,i);
数组复制(骰子,i+1,分辨率,i,骰子长度-i-1);
返回res;
}
骰子[]arr=null;
arr=removietem(arr,5);

由于您使用的是
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; 
} 
publicstaticdice[]removeTheElement(Dice[]arr,int index){
//如果数组为空
//或者索引不在数组范围内
//返回原始数组
如果(arr==null
||指数<0
||索引>=arr.length){
返回arr;
} 
//创建另一个大小小于1的数组
骰子[]另一个数组=新骰子[arr.length-1];
//复制除索引之外的元素
//从原始数组到另一个数组
对于(inti=0,k=0;i

因此在这里,我们将正在处理的
对象
和要删除的数组的索引作为参数传递。我们在函数内部创建一个临时的
对象
,并返回它,而不带我们提供的索引号的元素。

Java中的数组变量是指向一个内存区域的指针,该区域与它的容量和它包含的类型的大小成正比。无法动态修改此区域。唯一的方法是创建一个新的数组变量,该变量已分配了所需数组的新大小。这是在幕后发生的,也是在实现此功能的集合中发生的,只是为了仅在需要时执行此操作(可能非常昂贵)而进行了适当的优化


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

最好的办法是使用
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)

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

但是,我建议您应该使用ApacheCommons中的ArrayUtils

ArrayUtils.remove(array, index)
文档中的一些示例:

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

参考:

正如链接的问题所说:不。数组具有固定大小。你不想使用列表或其他收藏有什么原因吗?@VGR,不是真的。我正在上一门计算机科学课程,他们教你做事情的长途跋涉,即不使用方法。我只是想知道我自己的知识是不是可能我不确定你真正想要完成什么,但快速的回答是:不,在Java中数组有固定的大小,所以没有索引