我有我的自定义类的对象数组,我想删除一个随机对象(由某些条件选择)。我如何做到这一点,并保持阵列的秩序?自然,会有向左移动的元素,但我不完全删除元素部分,需要帮助来制定逻辑。这是我在做什么,但它不能正常工作:(从数组中删除对象
public static void deleteRecord(Customer[] records, int AccId){
int pos=0; // index
boolean found = false;
for (int i=0; i<count; i++){ // count is the number of elements in the array
if (records[i].get_accountid()==AccId){
found = true;
pos = i;
break;
}
}
if (!found)
System.out.println("The Record doesn't exist");
for (int j=pos+1; j<count; j++) {
records[j-1]= records[j];
}
如果您打算随机删除元素,通常应该使用列表。 – 2011-03-27 16:04:54
可能的重复[如何从java中的数组中删除对象?](http://stackoverflow.com/questions/112503/how-do-i-remove-objects-from-an-array-in-java) – McDowell 2011-05-09 10:26:02