简体   繁体   English

Java:从集合中删除元素

[英]Java: removing elements from a collection

I have a collection, and I want to implement the add() method, such that only positive integers can be added to the collection. 我有一个集合,并且我想实现add()方法,以便只能将正整数添加到该集合中。 The collection can hold 4 values, and I have used the code below to initialize every value as "-1". 该集合可以容纳4个值,我使用下面的代码将每个值初始化为“ -1”。

  public class Bag implements Collection {


private int[] elements;

public Bag() {
    elements = new int[Runner.SIZE_OF_COLLECTION];
    for (int i = 0; i < Runner.SIZE_OF_COLLECTION; i++) {
        elements[i] = -1;
    }
}

So far in the method add() below, I have this loop iterating through each element in the collection, and replacing each element that's less than 0 with the positive integer that I want to add ("toAdd"). 到目前为止,在下面的方法add()中,我循环遍历了集合中的每个元素,并用要添加的正整数(“ toAdd”)替换了小于0的每个元素。

The problem is, I only want to add the positive integer "toAdd" once, and without a break in the loop, the method replaces EVERY element "-1" in the collection with the positive integer. 问题是,我只想添加一次正整数“ toAdd”,并且在循环中不中断,该方法将正整数替换集合中的每个元素“ -1”。 With the break in the loop, the method fails to add the positive integer at all. 随着循环的中断,该方法根本无法添加正整数。 Any ideas on how I can get the method to add the positive integer to the collection only once? 关于如何获取将正整数仅添加到集合一次的方法有任何想法吗?

public void add(int toAdd) {
    for (int i = 0; i < Runner.SIZE_OF_COLLECTION; i++) {
        if (elements[i] <= 0 && toAdd>0) {
            elements[i] = toAdd;
            }
                        break;
    }
}

Thanks in advance! 提前致谢!

将中断移动到if语句中。

You could use an ArrayList instead of an int array. 您可以使用ArrayList代替int数组。 With an ArrayList, you could get the index of the first occurence of -1 and use the set method to add your new value at that index. 使用ArrayList,可以获得第一次出现-1的索引,并使用set方法在该索引处添加新值。

This replaces the first value from elements , which is equal or less than 0, with value from toAdd argument. 这将使用toAdd参数的值替换elements中等于或小于0的第一个值。

public void add(int toAdd) {
    for (int i = 0; i < Runner.SIZE_OF_COLLECTION; i++) {
        if (elements[i] <= 0 && toAdd>0) {
            elements[i] = toAdd;
            break;
        }             
    }
}

The add method for the interface Collection takes an Object (or a generic type, which you have not specified). 接口Collection的add方法采用一个Object(或未指定的通用类型)。 If you are trying to override / implement the collection interface method with your add method, then the method signature is incorrect, and it will never be called. 如果您尝试使用add方法覆盖/实现collection接口方法,则该方法签名不正确,并且将永远不会被调用。

Your class needs to look like: 您的课程需要看起来像:

public class Bag implement Collection<Integer>
{
  // ... other necessary methods

  public boolean add(Integer i)
  {
    // your method...
  } 
}

And probably easier than your implementation would be to do: 可能比实现起来容易:

public class Bag extends java.util.ArrayList<Integer>
{
  @Override
  public boolean add(Integer i)
  {
    if ((i != null) && (i > 0)) super.add(e);
  }
}

You probably need to override the other add methods as well, although truthfully, it would be better to encapsulate the ArrayList instead of extending it. 您可能还需要重写其他add方法,尽管实际上,最好封装ArrayList而不是对其进行扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM