How check if array is full and if not full add to it?

£可爱£侵袭症+ 提交于 2019-12-24 18:33:23

问题


I've got array. I've got an isFull method, which checks if the array is full, but I don't know how to use this to check if it's full, then if it's not full add to the array, otherwise disregard the add call.

The array should take 10 elements and then not accept any more. After 10 elements, it should 'be full' and disregard any addSpy calls.

How would you implement this?

public class ConcreteSubject extends AbstractSubject {

    public int arySize;
    private int i = 0;

    private static AbstractSpy[] spies;

    public ConcreteSubject(int a) {
        arySize = a;
        spies = new AbstractSpy[a];
    }

    @Override
    public void addSpy(AbstractSpy spy) {
        if (spies.length < 10) {
            spies[i] = spy;
            System.out.println("spy added at index " + i);
            i++;
        }
    }

    public void isFull() {
        //1
        boolean b = false;

        for (int i = 0; i < spies.length; i++) {
            if (spies[i] == null) {
                b = true;
            }
        }

        if (!b) {
            System.out.println("Array is full");
        } else {
            System.out.println("Array not full");
        }
    }



    public class TestSpies {

        public static void main(String[] args) {

            ConcreteSubject cs = new ConcreteSubject(10);
            AbstractSpy spy = new ConcreteSpy();

            AbstractSpy[] spies = new AbstractSpy[10];

            cs.addSpy(spy);
            cs.addSpy(spy);
            cs.addSpy(spy);

            cs.isFull();

        }
    }

回答1:


The simplest way of doing it would be like that:

@Override
public void addSpy(AbstractSpy spy) {
  if (!isFull())
  {
    spies[i] = spy;
    System.out.println("spy added at index " + i);
    i++;
  }
}

To use that, you should change your isFull method to:

public boolean isFull() {


  for (int i = 0; i < spies.length; i++) {
     if (spies[i] == null) {
         return false;
     }
  }

  return true;
}



回答2:


  1. spies.length < 10 isn't correct. It should be spies.length > 0 && i < spies.length to make sure that the following assignment spies[i] = spy; is always valid.

  2. void isFull() should be boolean isFull(). Your implementation looks OK, just return b. full is a tricky word because technically an array is always "full". A better adjective would be populated, filled.

  3. Since addSpy isn't filling null gaps but simply adds a spy to the end, isFull could be rewritten to return spies.length == i;.




回答3:


Keep a track of the number of filled cells of the array using a variable. And before inserting anything into it, check if the filled cells count strictly less than the size of the array (obviously you want to keep track of the array total size as well).



来源:https://stackoverflow.com/questions/58862781/how-check-if-the-array-is-full-and-only-add-to-it-if-its-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!