This is what i have so far but instead of just adding the object for some reason a 10 also appears before the added object.
public void addObject(Object object) {
/**
* Add object to myObjects and update currentObject. If there is no room
* in myObjects, the array will have to be resized by adding
* ARRAY_EXPAND entries.
*/
if (currentObject >= myObjects.length) {
Object[] newObjects = Arrays.copyOf(myObjects, (myObjects.length + ARRAY_EXPAND));
myObjects[currentObject] = object;
myObjects = newObjects;
}
currentObject++;
}
i am printing out the array using this method i created
public void showAll() {
for (int i = 0; i < myObjects.length -1; i++) {
if (myObjects[i] != null) {
System.out.println(myObjects[i].toString());
}
}
}
This is how im testing it
Object[] array1 = {0,1,2,3,4,5,6,7,8,9,10};
ObjectArray testArray1 = new ObjectArray(array1);
testArray1.showAll();
testArray1.addObject(5);
testArray1.showAll();
This is the output i get:
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
10
5
That last 10 before the 5 should not be there, it should be:
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
5
PS. DO NOT mention anything about ArrayList and using array.add(); I already know about this.