1

Hello i need to manually implement an arraylist.add() method using nothing but arrays and an array copy method but im having trouble doing it . The specification of the method is that the method inserts an element at a specified position and shifts any of the elements currently in the position to the right and add one to the indices expanding the size of the array by one so all elements fit . Someone please help .

    private Object [] list;
    final int maxObjects = 100;

    public ListOfObjects()
    {
        list= new Object[maxObjects];
    }
    public ListOfObjects(Object[]o)
    {
        list= o;

    }
    public void add(Object element,int index)
    {
        Object[] newData = new Object[list.length+1];
        for(int i =0; i < index; i++)
        {
            newData[i] = list[i];
            newData[list] = element;
        }

        for(int i = index; i < list.length; i++)
        {
            newData[i+1] = list[i];
        }
    }
1
  • Tip1: use System.arraycopy. Tip2: Indent your code properly! Commented Feb 7, 2015 at 18:57

4 Answers 4

0

Adding an element to an index of an Object array,

Object[] myObjects;

public static void addObject(Object obj, int index) {

// Assuming you want something in your empty array
     if(myObjects == null) {
        myObjects = new Object[] { obj };
        return;
    } 
    ArrayList<Object> temp = new ArrayList<Object>();
    for(int i = 0; i < myObjects.length; i++) { 
        if(i == index)
           temp.add(obj);
        temp.add(myObjects[i]);
    }
    myObjects = temp.toArray(new Object[temp.size()]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

The javadoc of System.arrayCopy speaks specifically to the case of the src and dest being the same array:

If the src and dest arguments refer to the same array object, then the copying is performed as if the components at positions srcPos through srcPos+length-1 were first copied to a temporary array with length components and then the contents of the temporary array were copied into positions destPos through destPos+length-1 of the destination array.

If your backing list is of sufficient size, then you simply need to use arrayCopy to move the affected indexes over 1.

//shift everything after the index over
System.arrayCopy(list, index, list, index + 1, list.length - index);
//place new value in index
list[index] = element;

Otherwise you need to create a new array, then use arrayCopy to copy everything before the inserting index.

Object[] newList = new Object[calcSize()];
//first copy everything before index if index is not 0
if (index > 0)
{
    System.arrayCopy(list, 0, newList, 0, index);
}
newList[index] = element;
System.arrayCopy(list, index, newList, index+1, list.length - index);

1 Comment

could I implement this into my current code?
0

You logic looks wrong to me. You should do something like -

    Object[] newData = new Object[list.length+1];
    for(int i =0; i < index; i++)
    {
        newData[i] = list[i];
    }
    newData[index] = element;
    for(int i = index; i < list.length; i++)
    {
        newData[i+1] = list[i];
    }

5 Comments

Do you know how I could implement system.arraycopy into this so i could expand the size?
system.arraycopy is used to copy arrays from a source to destination. you will have to do that separately for index : i=0 to i=index-1 and then from index : i=index+1 to i=newData.length-1
when testing this method i always get this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at newData[i+1] = list[i] at cse214hw1.CardCollection.main(CardCollection.java:139)
My mistake. It should be list.length in 2nd for loop.
where should I put the array.system copy? i currently have it after the forloops System.arraycopy(list, index, newData, index+1, list.length - index);
0

This solution takes advantage of the ArrayList iterator, which returns objects in the proper sequence:

    ArrayList<Object> elementInserter(ArrayList<Object> inArray, Object element, int index){
       ArrayList<Object> outArray = new ArrayList<Object>(inArray.size() + 1);
       outArray.addAll(inArray.subList(0, index));
       outArray.add(element);
       outArray.addAll(inArray.subList(index, inArray.size()));
       return outArray;
   }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.