Dynamic array in java

What I'm trying to do is

...
int sum[];
...
for(int z.....){
   ...
   sum[z] = some_random_value;
   ...
}

But it gives an error in the line sum[z]=ran;that the variable summay not have been initialized.

I tried int sum[] = 0;instead int sum[];, but even that gave an error. (I'm basically a C programmer)

+3
source share
5 answers

A dynamic size array is not possible in Java - you need to either know the size before declaring it, or perform resizing operations in the array (which can be painful).

Use instead ArrayList<Integer>, and if you need it as an array, you can return it.

List<Integer> sum = new ArrayList<>();
for(int i = 0; i < upperBound; i++) {
    sum.add(i);
}
// necessary to convert back to Integer[]
Integer[] sumArray = sum.toArray(new Integer[0]); 
+11
source

This is to fix compile time error:

int sum[] = null;

, , :

int[] sum = new int[10];

.

, List, ArrayList.

+5
int sum[]= new int[length];

. .

, length array .

int sum[] = null;, NullPointerException, sum[z]=ran;

?

. . Collection java. , A List ArrayList,

. , null.

int[] anArray = new int[10];, ,

10 anArray.

, java. . http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

+3

: int sum[]= new int[length];.

, .

, ArrayList toArray , .

+2

,

public class DynArray {
    private int size; // The current size of the array (number of elements in the array)
    private int maxSize; // Size of memory allocated for the array
    private Object[] array; // size of array == maxSize  

    /**
     * An array constructor
     * Argument specifies how much memory is needed to allocate for elements 
     * 
     * @param sz
     * @throws IndexOutOfBoundsException
     */
    public DynArray(int sz) throws IndexOutOfBoundsException {
        // Here called another more general constructor
        this(sz, sz, null);
    }
    /**
     * Call the constructor, in which indicated how much memory is allocated 
     * for the elements and how much memory is allocated total.
     * 
     * @param sz
     * @param maxSz
     * @throws IndexOutOfBoundsException
     */
    public DynArray(int sz, int maxSz) throws IndexOutOfBoundsException {
        // Here called another more general constructor     
        this(sz, maxSz, null);
    }
    /**
     * Additional argument contains an array of elements for initialization
     * 
     * @param sz
     * @param maxSz
     * @param iniArray
     * @throws IndexOutOfBoundsException
     */
    public DynArray(int sz, int maxSz, Object[] iniArray) throws IndexOutOfBoundsException {
        if((size = sz) < 0) { 
            throw new IndexOutOfBoundsException("Negative size: " + sz);
        }

        maxSize = (maxSz < sz ? sz : maxSz);
        array = new Object[maxSize]; // memory allocation

        if(iniArray != null) { // copying items
            for(int i = 0; i < size && i < iniArray.length; i++) {
                array[i] = iniArray[i];
                // Here it was possible to use the standard method System.arraycopy
            }
        }
    }
    /**
     * Indexing
     * 
     * @param i
     * @return
     * @throws IndexOutOfBoundsException
     */
    public Object elementAt(int i) throws IndexOutOfBoundsException {
        if (i < 0 || i >= size) {
            throw new IndexOutOfBoundsException("Index" + i + 
                    " out of range [0," + (size - 1) + "]");
        }
        return array[i];
    }
    /**
     * Changing the current size of the array. argument delta specifies
     * direction of change (positive - increase the size;
     * negative -  decrease the size)
     * 
     * @param delta
     */
    public void resize(int delta) {
        if (delta > 0) enlarge(delta); // increasing the size of the array 
        else if (delta < 0) shrink(-delta); // decreasing the size of the array
    }
    /**
     * Increasing the size of the array
     * 
     * @param delta
     */
    public void enlarge(int delta) {
        if((size += delta) > maxSize) { 
            maxSize = size;
            Object[] newArray = new Object[maxSize];

            // copying elements
            for(int i =0; i < size - delta; i++)
                newArray[i] = array[i];

            array = newArray;
        }
    }
    /**
     * Decreasing the size of the array
     * 
     * @param delta
     */
    public void shrink(int delta) {
        size = (delta > size ? 0 : size - delta);
    }
    /**
     * Adding a new element
     * (with a possible increasing the size of the array)
     * 
     * @param e
     */
    public void add(Object e) {
        resize(1);
        array[size-1] = e;
    }   
    /**
     * Removing the given value - shifting elements and subsequent 
     * reduction the size of the array
     * 
     * @param e
     */
    public void remove(Object e) {
        int j;
        for(j = 0; j < size; j++) {
            if(e.equals(array[j])) {
                break;
            }
        }

        if(j == size) {
            return false;
        } else {
            for(int k = j; k < size; k++) 
                array[k] = array[k + 1]; 

            resize(-1);
            return true;
        }
    }
}
+1

All Articles