gpt4 book ai didi

java - 从数组中删除一些元素后如何减小数组的大小

转载 作者:行者123 更新时间:2023-12-02 11:40:39 25 4
gpt4 key购买 nike

我正在开发一个项目,这是任务之一:创建一个名为“remove()”的方法,该方法可以从数组中删除元素。删除后,如果元素数量小于数组的1/4,则数组大小需要减少一半。

例如:我有一个大小为 100 的数组,其中包含 25 个元素。删除 1 个元素后,我将拥有 24 个元素,数组的大小将为 50。

这是我的代码:

    //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();
}

经过一些测试运行后,我的删除工作正常,无论我放入什么大小,数组长度都不会减少。

有人可以帮助我吗?谢谢

最佳答案

此外,您应该只使用 if (numLength < (Array.length/4)) 而不是 (1/4) * (Array.length);不要做任何奇怪的 Actor 或类似的事情。默认情况下,如果这是您期望的行为,java 整数除法会将结果取整。

此外,您应该能够仅使用一些 Arrays.copyOfRange 和 System.arraycopy 来实现您的复制需求。

https://docs.oracle.com/javase/7/docs/api/java/lang/System.html https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

这是一个简单的代码片段,基本上实现了从数组中删除元素。

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);
}
}

这个示例在刚运行时会打印出来

===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,

关于java - 从数组中删除一些元素后如何减小数组的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48592880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com