J'essaie de supprimer des éléments d'un tableau générique en Java.Suppression d'éléments d'un tableau générique
Par exemple, un tableau [1,2,3,4]
deviendrait [null,null,null,4]
si un 3 était passé pour le paramètre n
.
Mon problème est que ma méthode ne fait rien pour le tableau. Y a-t-il une meilleure manière de faire cela?
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()));
}
}
sortie réelle:
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]
Résultats escomptés:
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]
J'ai un constructeur. Ici c'est –
public Test (capacité int) { this.queue = (E []) new Objet [capacité]; } –
Désolé, où? Pouvez-vous l'ajouter à votre code? –