Variable length (Dynamic) Arrays in Java

JavaArraysDynamicArraylist

Java Problem Overview


I was wondering how to initialise an integer array such that it's size and values change through out the execution of my program, any suggestions?

Java Solutions


Solution 1 - Java

Yes: use ArrayList.

In Java, "normal" arrays are fixed-size. You have to give them a size and can't expand them or contract them. To change the size, you have to make a new array and copy the data you want - which is inefficient and a pain for you.

Fortunately, there are all kinds of built-in classes that implement common data structures, and other useful tools too. You'll want to check the Java 6 API for a full list of them.

One caveat: ArrayList can only hold objects (e.g. Integers), not primitives (e.g. ints). In MOST cases, autoboxing/autounboxing will take care of this for you silently, but you could get some weird behavior depending on what you're doing.

Solution 2 - Java

Arrays in Java are of fixed size. What you'd need is an ArrayList, one of a number of extremely valuable Collections available in Java.

Instead of

Integer[] ints = new Integer[x]

you use

List<Integer> ints = new ArrayList<Integer>();

Then to change the list you use ints.add(y) and ints.remove(z) amongst many other handy methods you can find in the appropriate Javadocs.

I strongly recommend studying the Collections classes available in Java as they are very powerful and give you a lot of builtin functionality that Java-newbies tend to try to rewrite themselves unnecessarily.

Solution 3 - Java

Arrays are fixed size once instantiated. You can use a List instead.

Autoboxing make a List usable similar to an array, you can put simply int-values into it:

List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);

Solution 4 - Java

I disagree with the previous answers suggesting ArrayList, because ArrayList is not a Dynamic Array but a List backed by an array. The difference is that you cannot do the following:

ArrayList list = new ArrayList(4);
list.put(3,"Test");

It will give you an IndexOutOfBoundsException because there is no element at this position yet even though the backing array would permit such an addition. So you need to use a custom extendable Array implementation like suggested by @randy-lance

Solution 5 - Java

Simple code for dynamic array. In below code then array will become full of size we copy all element to new double size array(variable size array).sample code is below 

public class DynamicArray {
 static   int []increaseSizeOfArray(int []arr){
          int []brr=new int[(arr.length*2)];
          for (int i = 0; i < arr.length; i++) {
         brr[i]=arr[i];     
          }
          return brr;
     }
public static void main(String[] args) {
     int []arr=new int[5];
      for (int i = 0; i < 11; i++) {
          if (i<arr.length) {
              arr[i]=i+100;
          }
          else {
              arr=increaseSizeOfArray(arr);
              arr[i]=i+100;
          }        
     }
      
for (int i = 0; i < arr.length; i++) {
     System.out.println("arr="+arr[i]);
}    
}

}

Source : How to make dynamic array

Solution 6 - Java

  1. It is recommend to use List to deal with small scale size.

  2. If you have a huge number of numbers, NEVER use List and autoboxing,

    List< Integer> list

For every single int, a new Integer is auto created. You will find it getting slow when the size of the list increase. These Integers are unnecessary objects. In this case, to use a estimated size would be better,

int[] array = new int[ESTIMATED_SIZE];

Solution 7 - Java

You can't change the size of an array. You can, however, create a new array with the right size and copy the data from the old array to the new.

But your best option is to use IntList from jacarta commons. (here)

It works just like a List but takes less space and is more efficient than that, because it stores int's instead of storing wrapper objects over int's (that's what the Integer class is).

Solution 8 - Java

How about using a List instead? For example, ArrayList<integer>

Solution 9 - Java

I answered this question and no you do not need an arraylist or any other thing this was an assignment and I completed it so yes arrays can increase in size. Here is the link https://stackoverflow.com/questions/14864815/how-to-use-java-dynamic-array/15990741#15990741 and here is the link for my question which i answered https://stackoverflow.com/questions/15891395/java-dynamic-arrays-answered

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMohammad SepahvandView Question on Stackoverflow
Solution 1 - JavaPopsView Answer on Stackoverflow
Solution 2 - JavaMattGrommesView Answer on Stackoverflow
Solution 3 - JavaMnementhView Answer on Stackoverflow
Solution 4 - JavacspannView Answer on Stackoverflow
Solution 5 - JavaAnuj DhimanView Answer on Stackoverflow
Solution 6 - JavaHao DengView Answer on Stackoverflow
Solution 7 - JavaThiago ChavesView Answer on Stackoverflow
Solution 8 - JavaKonrad GarusView Answer on Stackoverflow
Solution 9 - JavaNobodyView Answer on Stackoverflow