0

I want to create an extendable array.

Whenever you try and add an element into the array and it's full:

  • it should copy every element from the current array into the new one
    (including the new element that you want to add),

  • then it should delete the old array.

Would anyone be able to give me some pointers on how to do this?

EDIT:
Note: I can't use an ArrayList for this.

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Alex
  • 627
  • 3
  • 12
  • 32
  • 4
    you can use ArrayList for it. It is extendable. – Abubakkar Oct 14 '12 at 16:04
  • [`ArrayList`](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html) is what you want. – Rohit Jain Oct 14 '12 at 16:04
  • 4
    Why can't you use `ArrayList`? – Baz Oct 14 '12 at 16:06
  • 3
    If you can't use ArrayList (why?) then take a look at the ArrayList source code, since this does what you need (although it doesn't copy the array on *every* addition) and write your own version based upon it. – DNA Oct 14 '12 at 17:50

6 Answers6

3

If you can't use ArrayList, then you'll have to write your own class that basically does the same thing as ArrayList.

Your class will need to hold a reference to an array, as well as an integer that says how many values have been put into it (since it may be less than the current size of the array).

When an element is added to the array, check that integer against the array's current size. If it's less than the array's size, it means there's room for another element, so just put the new value into the array using the integer as the index, then add one to the integer. If the integer is equal to or greater than the array's size, it means you need a bigger array, and you'll have to write the code to create a new array and copy all the values to it.

When you create a bigger array, you'll probably want to make it big enough to hold more than one additional value, so that you don't have to create new arrays (and copy lots of values) every time you add a new element. A common technique is to make the new array size some percentage of the old one, like 150%.

Wyzard
  • 33,849
  • 3
  • 67
  • 87
1

This code was not tested but it should give you a starting point on what you should do. I implemented only on Integer but i trust you can extend this even more.

public class ExtendableArray {

    private Integer[] arr;

    public ExtendableArray(int capacity) {
        arr = new Integer[capacity];
    }

    public void add(Integer item) {
        if (getLastIndex() == arr.length) {
            generateBiggerArray();
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == null) {
                arr[i] = item;
                break;
            }
        }
    }

    private void generateBiggerArray() {
        int currentCapacity = arr.length;
        Integer[] tempArr = new Integer[currentCapacity + 4];
        for(int i = 0; i < arr.length; i++) {
            tempArr[i] = arr[i];
        }
        this.arr = tempArr;
    }

    private int getLastIndex() {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == null)
                return i;
        }
        return arr.length;
    }
}

BTW - there are many places in this code that you can improve (efficiency etc..) i encourage you to try. This is a good exercise :)

Mr T.
  • 4,278
  • 9
  • 44
  • 61
0

You should use an ArrayList. See this tutorial.

If you can't use an array list, from your edit, then you need to loop the array and move each element into a bigger array based on the length of you current array.

A simple google search should answer this.

chantheman
  • 5,256
  • 4
  • 23
  • 34
0

Use ArrayList check the javadoc, I think it is what are you looking for

Federico Lenzi
  • 1,632
  • 4
  • 18
  • 34
0

I recommend you to check the source code of the ArrayList class.

ElderMael
  • 7,000
  • 5
  • 34
  • 53
0

As others recommended, use ArrayList if possible. But if you can't for some reason (I guess homework?), then have a look at System.arraycopy to efficiently copy arrays.

Puce
  • 37,247
  • 13
  • 80
  • 152