gpt4 book ai didi

java - 从通用数组中删除项目

转载 作者:行者123 更新时间:2023-12-02 12:07:21 24 4
gpt4 key购买 nike

我正在尝试从 Java 中的通用数组中删除项目。

例如数组 [1,2,3,4]将变成[null,null,null,4]如果 n 传入 3参数。

我的问题是我的方法对数组没有执行任何操作。有一个更好的方法吗?

import java.util.Arrays;
import java.util.stream.Collectors;

public class Test<E>
{



private E[] queue;
private int added = 0;
private int removed = 0;


@SuppressWarnings("unchecked")
public Test(int capacity){
this.queue=(E[]) new Object[capacity];

}


public E[] getQueue(){
return queue;
}


public void setQueue(E[] items){
queue = items;
added=items.length;
removed = 0;

}


public static String formatOutput(Object[] items){
if (items == null) return "null";
return String.format("[%s]\n",
String.join(", ",
Arrays.stream(items).map(o -> ((o != null) ? o.toString() : "null")).collect(Collectors.toList())));
}


public E[] newArray(int capacity){

@SuppressWarnings("unchecked")
E[] a= (E[]) new Object[capacity];
return a;
}


@SuppressWarnings("unchecked")
public E[] removeNItems(int n){
E[] a= (E[]) new Object[n];
if(n<queue.length){
return null;
}
else{
for(int i=0; i<n;i++){
a= (E[]) queue[i];
queue[i]=null;

this.removed++;

}}


return a;

}





public boolean addNItems(E[] items){
if(items.length+this.added>this.queue.length){


return false;
}
else{
for(int i=0; i<items.length;i++){
queue[i]= (E) items[i];
added++;
}



return true;
}
}




public int size(){
return added - removed;
}


public static void main(String [] args){
Test<Integer> test = new Test<>(10);
System.out.print("Testing the constructor with a capacity of 10: " + formatOutput(test.getQueue()));
System.out.println("Size of queue after operation: " + test.size());
// testing the newArray method
System.out.println("Testing the newArray method with a capacity of 5: " + formatOutput(test.newArray(5)));

System.out.println("Trying to add 5 items");
Integer[] addFive = {1, 2, 3, 4, 5};
System.out.println("Able to add 5 items-> " + test.addNItems(addFive));
System.out.println("Size of queue after operation: " + test.size());
System.out.println("Array after adding 5 ints: " + formatOutput(test.getQueue()));

System.out.println("Trying to remove 4 items");
Object[] fourItemsRemoved = test.removeNItems(4);
System.out.println("Items removed: " + formatOutput(fourItemsRemoved));
System.out.println("Size of queue after operation: " + test.size());
System.out.println("Array after trying to remove four items: " + formatOutput(test.getQueue()));



}
}

实际输出:

   Trying to remove 4 items
Items removed: null
Size of queue after operation: 5
Array after trying to remove four items: [1, 2, 3, 4, 5, null, null,null, null, null]

预期输出:

Trying to remove 4 items
Items removed: [1,2,3,4]
Size of queue after operation:6
Array after trying to remove four items: [null,null,null,null,5,6]

最佳答案

使用以下数组方法仅切出您想要的部分,对于您的用例来说,该部分似乎是 n 到末尾。

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#copyOfRange(T[],%20int,%20int)

E[] newQueue = Arrays.copyOfRange(queue, n, queue.length)

关于java - 从通用数组中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46801350/

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