随机选择非完整数组中的数组元素

时间:2017-03-20 17:35:19

标签: java arrays arraylist

我有一个程序,我正在创建arrayList。一种方法从列表中随机删除一个项目。如果添加了一定数量的元素,我还有一个方法可以调整数组大小,以便在需要时添加更多元素。

我在调整列表大小时收到NullPointerExceptionError,然后该方法尝试删除新调整大小的数组中的null个空格之一。

如果不使用for循环并保持一个固定的时间,我正在尝试找到一种方法,使int的{​​{1}}值choice只是非null元素的数量阵列。我希望这是有道理的 - 任何人都可以帮忙吗?

remove方法:

public T remove() {
  if (isEmpty()) {
     return null;
  }
  Random rand = new Random();
  int choice = rand.nextInt(size);
  size--;
  elements[choice] = elements[size];
  elements[size] = null;
  return elements[choice];
} 

1 个答案:

答案 0 :(得分:2)

这个问题可以归结为“给定一个遍布零元素的数组,如何返回第n个非null元素?”答案是遍历数组,这不会维持您正在寻找的恒定时间约束。

但是等等,阵列列表在这种情况下如何工作?!? 以下是java.util.ArrayList的用法:

@Override
public E get(int location) {
    if (0 <= location && location < (lastIndex - firstIndex)) {
        return (E) array[firstIndex + location];
    }
    throw new IndexOutOfBoundsException(Messages.getString("luni.0A",
            location, lastIndex - firstIndex));
}

请注意,没有空检查,Arraylist始终将其元素保持在一个连续的块中,因此获取超快。因此,在删除元素时必须发生一些魔法,因此不会有空的分散......

@Override
public E remove(int location) {
    E result;
    int localLastIndex = lastIndex;
    int size = localLastIndex - firstIndex;
    if (0 <= location && location < size) {
        if (location == size - 1) {
            result = (E) array[--localLastIndex];
            array[localLastIndex] = null;
        } else if (location == 0) {
            result = (E) array[firstIndex];
            array[firstIndex++] = null;
        } else {
            int elementIndex = firstIndex + location;
            result = (E) array[elementIndex];
            if (location < size / 2) {
                System.arraycopy(array, firstIndex, array, firstIndex + 1,
                        location);
                array[firstIndex++] = null;
            } else {
                System.arraycopy(array, elementIndex + 1, array,
                        elementIndex, size - location - 1);
                array[--localLastIndex] = null;
            }
        }
        if (firstIndex == localLastIndex) {
            firstIndex = localLastIndex = 0;
        }
    } else {
        throw new IndexOutOfBoundsException(Messages.getString("luni.0A",
                location, localLastIndex - firstIndex));
    }
    lastIndex = localLastIndex;
    modCount++;
    return result;
}

有钥匙!删除元素时,JDK impl不会留下间隙,也不会移动所有元素以缩小间隙。它调用一个本机方法,将一大块数组复制为原子操作:https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int)