Your implementation has a few errors.
public StringArrayList(int initialCapacity){
strings = new String[0];
}
This type of constructor is meant to allocate with the initial capacity, not with "0".
And most importantly, your add method doesn't actually add a new element, but overwrites your current internal array, and places a new element at the 0th index (without incrementing the size variable).
public boolean add(String content){
boolean add = false; //unnecessary, can replace bottom `add` with `false` for same result
strings = new String[10]; //overwrites internal array of "list" with a new array
for(int i = 0; i < strings.length; i++){
if(strings[i] == null) { //will always replace only first element as this is a new array
strings[i] = content;
return true;
}
}
return add;
}
The size variable is meant to keep track of how many elements the current array contains (which would be incremented when you add a new element).
Knowing that, you'd be able to add a new element instrings[size] to add a new element, in case size < strings.length was true.
If that condition is not met (you want to add more than the current capacity), it should "resize" the internal array by creating a new array, copying all previous elements into the new array, then replacing the old array, and adding the new element to the new array.
EDIT: Your new code
public boolean add(String content) {
strings = new String[strings.length+1]; //still erases the internal array!
strings[0] = content; //only modifies the first element, rather than add to the list
size = 1; //if it was implemented correctly, this would be size++;
return true;
}
Make a test that adds TWO elements to the list, and assert if the list contains both of them.
EDIT2:
Here is a solution that ought to work:
public class StringArrayList {
private int size = 0;
private String[] strings;
// Constructors
public StringArrayList() {
this(0);
};
public StringArrayList(int initialCapacity){
String[] innerStrings = new String[initialCapacity];
this(innerStrings);
}
public StringArrayList(String[] strings){
this.strings = strings;
}
public int size() {
return size;
}
public String get(int index){
return strings[index];
}
public boolean add(String content){
if(size == strings.length) {
String[] newStrings = new String[size+10];
for(int i = 0; i < size; i++) {
newStrings[i] = strings[i];
}
strings = newStrings;
}
strings[size++] = content;
return true;
}
}
EDIT3:
public boolean add(String content){
if(size == strings.length) {
String[] temp_list = new String[strings.length]; //no need to allocate a new array here if this is just to store your current array
temp_list = strings;
strings = new String[size++]; //wrong, this allocates a `size`-long array and increases `size` by 1 afterwards, rather than create a new, larger internal array
strings = temp_list; //this just overwrites your internal array with the old array which is not increased in size
}
for(int i = 0; i < strings.length; i++) {
if(strings[i] == null) { //this is not necessary with proper `size`
strings[i] = content;
}
}
return true;
}
list()andlist(String)do?list()andlist(String str)?addmethod?