Q:

Java Arrays[] - Assigning Specific Elements in a For Loop to Certain Values

I was attempting to write some code for a program in BlueJ (Java) that lists bags and adds and removes items from those bags, that sort of thing. Then I got stuck in the first class; I couldn't get to add an item to the bag properly as you can notice below in the addItem() method; it keeps adding String s to every null element in the array rather the first encountered. Any help would be tremendously appreciated.
Best wishes & many thanks,
Xenos
public class Bag1 {
    private String[] store; // This is an array holding mutlitple strings.

    public Bag1(int storageCapacity) {
        store = new String[storageCapacity];
    } // That was the primitive array constructor.

    public boolean isFull() {
        boolean full = true;
        for(int i = 0; i < store.length; i++) {
            if(store[i] == null) {
                full = false;
            }
        }
        return full;
    } // The method above checks if the bag is full or not, and returns a boolean value on that basis.

    public void add(String s) {
        for(int i = store.length; i >= 0; i--) {
            if(store[i] == null) {
                store[i] = s;
            }
        }
    }
}

A:

You should exit the loop after finding the first empty spot :
public void add(String s) 
{
    for(int i=store.length-1; i>=0; i--) { // note the change in the starting index
        if(store[i]==null) {
            store[i] = s;
            break;
        }
    } 
}

