1

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]);
        }
    }


}
7
  • 1
    "I need to make my own data structures" why? You know the source of the provided array list is visible? Commented Dec 30, 2016 at 18:17
  • Probably an assignment Commented Dec 30, 2016 at 18:18
  • You have a loop whose purpose is to shift elements. The loop body will execute once for each element you need to shift. You don't want to create a new element every time you shift one, do you? So the code to add the new element needs to be moved out of the loop. Commented Dec 30, 2016 at 18:19
  • Can't you just view the source code of java.util.ArrayList and nick it? Commented Dec 30, 2016 at 18:19
  • I need to do it as an assignment. Commented Dec 30, 2016 at 18:24

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect answer. Just what I needed. Thank you for the guidance. Much appreciated.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.