在Java中你可以将其设置为空数组中删除元素? [英] In java to remove an element in an array can you set it to null?
本文介绍了在Java中你可以将其设置为空数组中删除元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使该对数组实现列表的工作的删除方法。
我可以设置重复的元素设置为null删除它?假设该名单正在有序进行。
ArrayList的一个=新的ArrayList [];公共无效removeduplicates(){ 用于:(a [i]于.equals(一个第[i + 1]){ 一个第[i + 1] =无效; }
一个第[i + 1] = A [];
}
解决方案
不,你不能从一个数组中删除元素,如使之短。 Java数组是固定大小。您需要使用 的ArrayList
了点。
如果您设置为null元素,阵列将仍然有相同的大小,但在这一点空引用。
//假设A = [0,1,2,3,4](整数[])
一个[2] = NULL;
//现在A = [0,1,空,3,4]
I am trying to make a remove method that works on an array implementation of a list. Can I set the the duplicate element to null to remove it? Assuming that the list is in order.
ArrayList a = new ArrayList[];
public void removeduplicates(){
for(a[i].equals(a[i+1]){
a[i+1] = null;
}
a[i+1] = a[i];
}
解决方案
No you can't remove an element from an array, as in making it shorter. Java arrays are fixed-size. You need to use an ArrayList
for that.
If you set an element to null, the array will still have the same size, but with a null reference at that point.
// Let's say a = [0,1,2,3,4] (Integer[])
a[2] = null;
// Now a = [0,1,null,3,4]
这篇关于在Java中你可以将其设置为空数组中删除元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文