-1

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.

user3439273
  • 125
  • 1
  • 2
  • 12

2 Answers2

4

What do you mean "this is the output I should get"?

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

You have an array with the numbers 0-10 and you add 5. The output you get is the output you should be getting.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
  • Im an idiot, in my actual code i have another array and i was focusing on the other array which is 1-9; and i thought thats the one i was using. – user3439273 Apr 20 '14 at 03:51
  • @user3439273 It happens to the best of us. Try to proofread before posting a question next time :) – Anubian Noob Apr 20 '14 at 03:52
  • 1
    lol trust me i thought i did, must be getting tired. – user3439273 Apr 20 '14 at 03:53
  • One last thing if you dont mind, it is working fine, but when i add two things for example: testArray1.addObject(5); testArray1.addObject(6);.....It still only shows 1,2,3,4,5,6,7,8,9,10,5 not 1,2,3,4,5,6,7,8,9,10,5,6 – user3439273 Apr 20 '14 at 03:58
1

Your showall method looks like it has a slight error

public void showAll() {
    for (int i = 0; i < myObjects.length -1; i++) {
        if (myObjects[i] != null) {
            System.out.println(myObjects[i].toString());
        }
    }
}

You probably want either

for (int i = 0; i <= myObjects.length-1; i++) {

or

for (int i = 0; i < myObjects.length; i++) {

since as it stands, you don't show the last object of the array

(I would have posted this as a comment and not an answer but I don't have enough reputation points)

user1813598
  • 148
  • 1
  • 8