Can you change size of Array in Java once created?

Java 8Object Oriented ProgrammingProgramming

An array is a data structure/container/object that stores a fixed-size sequential collection of elements of the same type. The size/length of the array is determined at the time of creation.

The position of the elements in the array is called as index or subscript. The first element of the array is stored at the index 0 and, the second element is at the index 1 and so on.

Each element in an array is accessed using an expression which contains the name of the array followed by the index of the required element in square brackets.

For example, if an array of 6 elements is created with name myArray, you can access the element of the array at index 3 as −

System.out.println(myArray[3]);
//25

Size of an array

In Java, arrays are treated as referenced types you can create an array using the new keyword similar to objects and populate it using the indices as −

The size of an array is fixed, if you create an array using the new keyword you need to specify the length/size of it in the constructor as −

int myArray[] = new int[7];
myArray[0] = 1254;
myArray[1] = 1458;
myArray[2] = 5687;
myArray[3] = 1457;
myArray[4] = 4554;
myArray[5] = 5445;
myArray[6] = 7524;

You can also directly assign values within flower braces separating them with commas (,) as −

int myArray = {1254, 1458, 5687, 1457, 4554, 5445, 7524}; //size 7

If you create an array by initializing its values directly, the size will be the number of elements in it.

Thus the size of the array is determined at the time of its creation or, initialization once it is done you cannot change the size of the array.

Still if you try to assign value to the element of the array beyond its size a run time exception will be generated.

Example

In the following example we are creating an array with 7 elements and alter we are trying to assign value to the 8th element.

public class Test {
   public static void main(String[] args) {
      int myArray[] = new int[7];
      myArray[0] = 1254;
      myArray[1] = 1458;
      myArray[2] = 5687;
      myArray[3] = 1457;
      myArray[4] = 4554;
      myArray[5] = 5445;
      myArray[6] = 7524;
      myArray[7] = 4238;
      System.out.println(Arrays.toString(myArray));
   }
}

Run time error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at Test.main(Test.java:13)

Changing the size of an array

You can change the size of the existing array by reassigning it to the new one as −

Example

import java.util.Arrays;
public class Test {
   public static void main(String[] args) {
      int myArray[] = new int[7];
      myArray[0] = 1254;
      myArray[1] = 1458;
      myArray[2] = 5687;
      myArray[3] = 1457;
      myArray[4] = 4554;
      myArray[5] = 5445;
      myArray[6] = 7524;
      System.out.println(Arrays.toString(myArray));
      myArray = new int[8];
      myArray[0] = 1254;
      myArray[1] = 1458;
      myArray[2] = 5687;
      myArray[3] = 1457;
      myArray[4] = 4554;
      myArray[5] = 5445;
      myArray[6] = 7524;
      myArray[7] = 3165;
      System.out.println(Arrays.toString(myArray));
   }
}

Output

[1254, 1458, 5687, 1457, 4554, 5445, 7524]
[1254, 1458, 5687, 1457, 4554, 5445, 7524, 3165]

In this scenario the earlier array object will be left out for garbage collection. Thus, when used extensively this approach causes memory issues therefore, it is not recommended.

raja
Published on 02-Aug-2019 17:56:49
Advertisements