Skip to main content

Copying and converting arrays (including ArrayList) in Java

 

Copying and converting arrays in java

Copying arrays is a simple operation, but must be done with caution. Doing it improperly may cause copying reference instead of actual array which may lead to a serious trouble. In this article I am explaining how to copy Java arrays the right and safe way. Additionally, I will explain how to easily convert array to ArrayList and opposite – ArrayList to array.

Copying array

The right way to copy an array is to use the static Arrays.copyOf method. This method is overloaded and allows to copy an array of any type.

Example code:



import java.util.Arrays;

public class Prototyping {
   public static void main(String[] args) {
      // Create a new array and copy it to another variable
      int[] fibo = new int[] {1,1,2,3,5};
      int[] fiboCopy = Arrays.copyOf(fibo,fibo.length);

      // Proof that we have made a copy the array
      fibo[0] = 123;
      System.out.println(Arrays.toString(fibo)); // prints [123, 1, 2, 3, 5]
      System.out.println(Arrays.toString(fiboCopy)); // prints [1, 1, 2, 3, 5]
   }
}

What you should keep in mind, is that you should never ever try to copy an array with = operator. By doing this, you will not make a copy of an array, but a copy of reference to your array.

Example of WRONG code:


import java.util.Arrays;

public class Prototyping {
   public static void main(String[] args) {
      // Never try to "copy" an array with = operator!
      int[] fibo = new int[] {1,1,2,3,5};
      int[] fiboCopy = fibo;

      // Proof that we have NOT copied an array
      // We have copied only the reference, therefore any change on
      // fibo or fiboCopy will be visible through both variables
      fibo[0] = 123;
      System.out.println(Arrays.toString(fibo)); // prints [123, 1, 2, 3, 5]
      System.out.println(Arrays.toString(fiboCopy)); // prints [123, 1, 2, 3, 5]
   }
}

Copying ArrayList

Copying an ArrayList is even more simple. All you have to do is provide the ArrayList object you want to copy to ArrayList constructor.

Example code:



import java.util.ArrayList;

public class Prototyping {
   public static void main(String[] args) {
      ArrayList<integer> fibo = new ArrayList<>();
      fibo.add(1);
      fibo.add(1);
      fibo.add(2);
      fibo.add(3);
      fibo.add(5);

      // Copy the ArrayList
      ArrayList<integer> fiboCopy = new ArrayList<>(fibo);

      // Proof that we have made a copy of ArrayList
      fibo.set(0, 123);
      System.out.println(fibo); // prints [123, 1, 2, 3, 5]
      System.out.println(fiboCopy); // prints [1, 1, 2, 3, 5]
   }
}



As with regular arrays, you should never ever try to copy an ArrayList with = operator. By doing this, you will not make a copy of an ArrayList object, but a copy of its reference. 

Example of WRONG code:




import java.util.ArrayList;

public class Prototyping {
   public static void main(String[] args) {
      ArrayList<Integer> fibo = new ArrayList<>();
      fibo.add(1);
      fibo.add(1);
      fibo.add(2);
      fibo.add(3);
      fibo.add(5);

      // Never try to "copy" an ArrayList with the = operator!
      ArrayList<Integer> fiboCopy = fibo;

      // Proof that we have NOT copied the ArrayList
      // We have copied only the reference, therefore any change on
      // fibo or fiboCopy will be visible through both variables
      fibo.set(0, 123);
      System.out.println(fibo); // prints [123, 1, 2, 3, 5]
      System.out.println(fiboCopy); // prints [123, 1, 2, 3, 5]
   }
}


Converting array to ArrayList

Converting a regular array into ArrayList can be easily done with ArrayList constructor and Arrays.asList static method.

Example code:



import java.util.ArrayList;
import java.util.Arrays;

public class Prototyping {
   public static void main(String[] args) {
      Integer[] fibo = new Integer[] {1,1,2,3,5};
      ArrayList<Integer> fiboCopy = new ArrayList<>(Arrays.asList(fibo));

      System.out.println(Arrays.toString(fibo)); // prints [1, 1, 2, 3, 5]
      System.out.println(fiboCopy); // prints [1, 1, 2, 3, 5]
   }
}


Converting ArrayList to array

Converting from ArrayList to a regular array is no more difficult as the opposite operation. We can achieve our goal with ArrayList.toArray() method.


import java.util.ArrayList;
import java.util.Arrays;

public class Prototyping {
   public static void main(String[] args) {
      ArrayList<Integer;> fibo = new ArrayList<>();
      fibo.add(1);
      fibo.add(1);
      fibo.add(2);
      fibo.add(3);
      fibo.add(5);

      Integer[] fiboCopy = fibo.toArray(new Integer[0]);

      System.out.println(fibo); // prints [1, 1, 2, 3, 5]
      System.out.println(Arrays.toString(fiboCopy)); // prints [1, 1, 2, 3, 5]
   }
}

You may be wondering what new Integer[0] does. The toArray() method requires to provide an array to which the ArrayList elements should be copied to. If the length of your ArrayList is unknown at the moment of writing the code, you can supply an array with a length of 0. The toArray() method will automatically create a new array with the right size. If you provide an array that is bigger than your ArrayList, the remaining space will be filled with null references.