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];
        }
    }
Brett Okken
  • 6,210
  • 1
  • 19
  • 25
nitin94
  • 11
  • 1
  • 1
  • 3
  • Tip1: use [System.arraycopy](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int)). Tip2: Indent your code properly! – Nir Alfasi Feb 07 '15 at 18:57

4 Answers4

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];
    }
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • Do you know how I could implement system.arraycopy into this so i could expand the size? – nitin94 Feb 07 '15 at 19:08
  • `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` – Aniket Thakur Feb 07 '15 at 19:14
  • 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) – nitin94 Feb 07 '15 at 19:39
  • My mistake. It should be `list.length` in 2nd for loop. – Aniket Thakur Feb 07 '15 at 19:58
  • 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); – nitin94 Feb 07 '15 at 20:28
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()]);
}
Ya Wang
  • 1,758
  • 1
  • 19
  • 41
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);
Brett Okken
  • 6,210
  • 1
  • 19
  • 25
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;
   }
Pete Adam
  • 1
  • 2