Java 如何在移除数组中的某些元素后减小数组的大小
我正在做一个项目,这是任务之一: 创建一个名为remove的方法,该方法可以从数组中删除元素。 移除后,如果元素数量小于阵列的1/4,则阵列的大小需要减少一半 例如: 我有一个大小为100的数组,其中包含25个元素。删除一个元素后,我将有24个元素,数组的大小将为50 这是我的密码:Java 如何在移除数组中的某些元素后减小数组的大小,java,arrays,Java,Arrays,我正在做一个项目,这是任务之一: 创建一个名为remove的方法,该方法可以从数组中删除元素。 移除后,如果元素数量小于阵列的1/4,则阵列的大小需要减少一半 例如: 我有一个大小为100的数组,其中包含25个元素。删除一个元素后,我将有24个元素,数组的大小将为50 这是我的密码: //First create a method decrese private decrease() { if (numElement < (1 / 4) * (Array.l
//First create a method decrese
private decrease() {
if (numElement < (1 / 4) * (Array.length)) {
Array[] newarray = new Array[(Array.length) / 2];
for (int i = 0; i < numElement; i++)
newarray[i] = Array[i];
Array = newarray;
}
//Then create my Remove method
public void remove(ToRemove){
if (numElement > 0) { //First check if my array is empty
for (int i = 0; i < numElement; i++) {
if (Array[i].equals(ToRemove)) {
Array[i] = Array[numElement - 1];
Array[numElement - 1] = null;
numElement--;
decrease();
}
}
//if the Array is empty, also decrease the size
decrease();
}
经过一些测试运行后,我的移除工作正常,数组长度永远不会减少,无论我放入的大小如何
谁能帮帮我吗。
谢谢简单的答案是你不能 Java数组数据结构的大小是固定的,一旦创建了相同的数组对象,就不能更改其大小
如果需要更改大小,则需要将其内容复制到新的数组对象,或者使用不同的数据结构,如ArrayList,它在内部执行此操作 另外,您应该只使用if numLength
import java.lang.reflect.Array;
import java.util.Arrays;
public class MySpecialArray<T> {
T[] buf;
int size;
Class<T> type;
public MySpecialArray(Class<T> type, int initialBufSize) {
this.size = 0;
this.type = type;
buf = (T[]) Array.newInstance(type, initialBufSize);
}
/**
* Like arraylist add, it will basically add freely until it reaches the max length of the buffer.
* Then it has to expand the buffer. It uses buf.length * 2 + 1 to account for when an initialBufSize of 0 is
* supplied.
* @param elem
*/
public void add(T elem) {
if (this.size == this.buf.length) {
int newSize = this.buf.length * 2 + 1;
buf = Arrays.copyOf(buf, newSize);
}
this.buf[this.size] = elem;
this.size += 1;
}
public void add(T...elements) {
for(T elem : elements) {
this.add(elem);
}
}
/**
* Remove all occurrences of an element. Also reduce the max buf_size of the array if my utilized size is less than a fourth of my max buf size.
* @param removeMe element to remove all occurrences of
* @return
*/
public void remove(T removeMe) {
boolean found = false;
for(int i = 0; i < this.size; i++) {
if (buf[i].equals(removeMe)) {
System.arraycopy(buf, i+1, buf, i, this.size - i);
this.size -= 1;
if (this.size < this.buf.length / 4) {
this.buf = Arrays.copyOf(buf, this.buf.length / 2);
}
}
}
}
/**
* Remove the last element
* @return
*/
public T remove() {
if (this.size == 0) {
throw new RuntimeException("Cannot remove from empty buffer");
}
T removed = this.buf[this.size -1];
this.size -= 1;
if (this.size <= this.buf.length / 4) {
int newSize = this.buf.length / 2;
this.buf = Arrays.copyOf(this.buf, newSize);
}
return removed;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < this.size; i++) {
sb.append(this.buf[i].toString()).append(",");
}
return sb.toString();
}
public static void main(String...args) {
MySpecialArray<Integer> arr = new MySpecialArray(Integer.class, 50);
arr.add(10, 2, 4, 3, 5, 11, 9, 3, 8, 16);
System.out.println("===Pre removed===");
System.out.println(arr.buf.length);
System.out.println(arr.size);
System.out.println(arr);
arr.remove(3);
System.out.println("===After removing 3===");
System.out.println(arr.buf.length);
System.out.println(arr.size);
System.out.println(arr);
}
}
Java数组具有固定的长度。您需要创建一个新的较小数组。可能与Tom的注释重复:您期望1/4=.25,但1和4是int类型,因此1/4实际上是0。由于您是对值进行硬编码,因此只需使用0.25即可。如果您使用的是变量,您将强制转换为双精度。@IanMc谢谢各位,我不敢相信我被困在这里3个小时。您可能需要复制它的内容到一个新的数组对象-OP已经这样做了…新数组的大小/长度将是OP给出的,不是吗?
===Pre removed===
50
10
10,2,4,3,5,11,9,3,8,16,
===After removing 3===
25
8
10,2,4,5,11,9,8,16,