从通用数组中删除项目

时间:2017-10-18 01:08:30

标签: java arrays generics

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

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

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

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]

2 个答案:

答案 0 :(得分:1)

使用以下数组方法将您想要的部分切片出来,对于您的用例,它显示为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)

答案 1 :(得分:0)

查看整个代码,我可以看到您从未初始化变量队列。在某些时候你必须这样做,你可以在开头或构造函数中完成它。  好了,现在我看到了更新,抛出一个数组的好方法是从0开始并转到长度 - 1(所以你没有得到索引超出限制的异常)..  在你的情况下,你应该通过两个方面提出问题,在&gt; queu.length并且如果正在增加的n在最后。 最重要的是:  你在if语句中检查了你的要求吗?

'if(queue.length + remove&gt; queu.length)'

那总是假的,你想要问一下,你将Reese的元素数量是否小于队列的长度?那将是: 'if(queue.length&gt; n)' 其余代码没问题