user1999
user1999

Reputation: 93

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

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();

        }
    }

Upvotes: 0

Views: 2689

Answers (3)

Andrew
Andrew

Reputation: 49626

  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;.

Upvotes: 3

Cosmin Ioniță
Cosmin Ioniță

Reputation: 4055

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;
}

Upvotes: 2

Shankha057
Shankha057

Reputation: 1369

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).

Upvotes: 0

Related Questions