Archeofuturist
Archeofuturist

Reputation: 215

Adding a value at an index in an array list

I need to make my own data structures and part of this is doing an ArrayList. I need to make sure I can add an object at element n while pushing all the others down. Here is my code. Right now it is adding the element twice. It is the function public ReturnObject add(int index, Object item). I need this function to add the object at the specified index and then shift the others down.

public class ArrayList implements List{
    public static final int CAPACITY=16;
    private int size = 0;
    private Object[] data;
    ReturnObjectImpl ro;

    //constructors
    public ArrayList() {
         data = new Object[CAPACITY];
        }                             //CONSTRUCTS LIST WITH DEFAULT CAPACITY
    public ArrayList(int capacity) { // constructs list with given capacity
        data = new Object[capacity];
        System.out.println("Created an ArrayList of capacity " + capacity);
    }



    public boolean isEmpty(){
        if(size == 0) {
        //  System.out.println("The list is empty");
            return true; 
        }
        return false;
    }

    public int size(){
        System.out.println("The ArrayList is not full, but currently has " + size + " indexs");
        return size;
    }

    public ReturnObject get(int index){
        ro = new ReturnObjectImpl(data[index]);

        return ro;

    }

    public ReturnObject remove(int index){
        return null;

    }

    public ReturnObject add(int index, Object item){
        if(index <= size && index < data.length){
            for (int x = size-1; x >= index; x--){
                data[x+1] = data[x];
                data[index] = item;
                ro = new ReturnObjectImpl(data[index]);
                size++;

            }
            System.out.println("Added to array at " + index);
        }
        return ro;

    }

    public ReturnObject add(Object item){
        if (data[0] == null){
            data[0] = item;
        } 
        //int adding = size + 1;
        data[size] = item;
        System.out.println("Added item to index " + size);
        size++;
        return null;
    }
    //added - but DELETE BEFORE SUBMITTING
    public void printAll(){
        for(int x = 0; x < data.length; x++){
            System.out.println(data[x]);
        }
    }


}

Upvotes: 1

Views: 1596

Answers (2)

MC Emperor
MC Emperor

Reputation: 22977

In addition to GhostCat's answer, instead of a for loop, you can just use System.arrayCopy() to 'move' the right part to the right. You only need to know whether your internal array (data) is already full or not. If it is, then you must expand the internal array.

System.arraycopy(this.data, insertIndex, this.data, insertIndex + 1, 1);

Some notes:

  • The code

    if (data[0] == null) {
        data[0] = item;
    }
    

    will throw an ArrayIndexOutOfBoundsException if ArrayList(0) was called.

  • The code

    if (size == 0) {
        // System.out.println("The list is empty");
        return true; 
    }
    return false;
    

    can be rewritten to

    return (size == 0);
    
  • You seem to omit more checks, like the check whether the internal array is full or not. Your current code does not expand the internal array, so if one ever adds more objects than the initial capacity (default 16), then an ArrayIndexOutOfBoundsException is thrown.

Upvotes: 2

GhostCat
GhostCat

Reputation: 140457

Obviously, when inserting an object into that array:

for (int x = size-1; x >= index; x--){
  data[x+1] = data[x];
  data[index] = item;

that should not happen within a loop! Insertion should happen exactly once, at the correct index! So even when you keep that loop to move the other elements, that last assignment should go after that "moving loop" is over.

So the point is: you should step back and carefully look what this loop is doing with its loop variable.

In other words: either take a piece of paper and "run" the code yourself; or run it in a debugger.

As this is probably some kind of homework activity, I will leave it with that; it should be really enough to get you going and help you fix your code.

Upvotes: 3

Related Questions