2017-10-18 89 views
0

我想从Java中的泛型数组中删除项目。从通用数组中删除项目

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

我的问题是我的方法没有对数组做任何事情。有一个更好的方法吗?

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] 

回答

0

寻找你的整个代码,我可以看到你从来没有初始化您的变量队列。在某些时候你必须这样做,你可以在开始或构造函数中完成它。 好吧,现在我看到更新,一个很好的方式去抛出一个数组是从0开始,并去的长度 - 1(所以你没有得到一个索引超出限制例外)。 在你的情况,你应该由两件事情来询问,在> queu.length中,并且如果增加的n是最后的。 最重要的是: 您是否在if语句中检查了您要求的内容?

“如果(queue.length +删除> queu.length)”

这将是永远假的,你要问,而是如果元素的数量,您将里斯小于队列的长度?那么这将是: “如果(queue.length> N)” 的代码的其余部分是确定

+0

我有一个构造函数。这里是 –

+0

public Test(int capacity){this.queue =(E [])new Object [capacity]; } –

+0

对不起,在哪里?你可以将它添加到你的代码? –