[英]How to decrease the size of an array after removing some elements from it
我正在一個項目上,這是任務之一:創建一個名為“ remove()”的方法,該方法可以從數組中刪除一個元素。 刪除后,如果元素數少於數組的1/4,則數組的大小需要減少一半。
例如:我有一個大小為100的數組,其中包含25個元素。 刪除一個元素后,我將擁有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); 不要進行任何奇怪的轉換。 默認情況下,如果這是您期望的行為,則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 Array數據結構的大小固定,一旦創建,就無法更改Same Array對象的大小。
如果需要更改大小,則需要將其內容復制到新的Array Object或使用內部執行此操作的其他數據結構(例如ArrayList)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.