Java

How to Append to an Array in Java

While updating data in Java, there can be instances where the developer needs to omit or append the data in accordance with the requirement. For instance, appending identical data in the same array to ease accessibility. In such cases, appending to an array in Java is of great aid in sorting the data and making it readable.

This blog will discuss the approaches to append to an array in Java.

How to Append to an Array Using Java?

To append to an array in Java, apply the following approaches combined with the “toString()” method.

Approach 1: Append to an Array in Java Using “ArrayList” Class and “add()” Method

The “toString()” method gives the string representation of an object. The “ArrayList” class is a resizable array, contained in the “java.util package” and “add()” is one of its methods utilized to add elements to the ArrayList.

Example
Let’s follow the below-provided code:

Integer arr[] = {1,2,3};
System.out.println("The given array is:"+ Arrays.toString(arr));
ArrayList arrayList = new ArrayList(Arrays.asList(arr));
arrayList.add(4);
arr = arrayList.toArray(arr);
System.out.println("The new array becomes: "+ Arrays.toString(arr));

In the above lines of code:

  • First of all, initialize an array named “arr” with integer values.
  • In the next step, the “Arrays.toString()” method gives a string representation of the array contents in case of “int” array and displays it.
  • After that, apply the “Arrays.asList()” method to transform from an array to a fixed-size list object.
  • Now, associate the “add()” method with the “arrayList” class to append the specified integer as its(method) parameter into the array. Also, convert the “arrayList” into an array via the associated “toArray()” method.
  • Lastly, apply the “Arrays.toString()” method again to transform the array values into string and display it.

Output

In this output, it can be seen that a new integer is appended to the array, successfully.

Approach 2: Append to an Array in Java Using “length” Attribute and “for” Loop

The “length” attribute computes the array length and the “for” loop is utilized to iterate through the items and perform the functionality accordingly. These approaches can be applied in combination with the “toString()” method to determine and specify the array length and append a new value to the array based on that.

Example
Let’s overview the below-provided example:

int givenArray[] = {1,2,3,4,5,6};
int n = givenArray.length;
int updatedArray[] = new int[n+1];
int value = 7;
System.out.println("The given Array is: "+ Arrays.toString(givenArray));
for(int i = 0; i<n; i++) {
updatedArray[i] = givenArray[i];
}
updatedArray[n] = value;
System.out.println("The new array becomes: "+ Arrays.toString(updatedArray));

In the above code-snippet:

  • Firstly, declare an array having integer values.
  • In the next step, associate the “length” attribute to compute the array’s length.
  • After that, the “new int[ ]” allocates the array length by adding “1” to the current array length to accumulate the value that needs to be appended.
  • Now, specify the integer that needs to be appended. Also, transform the array values into string via the “toString()” method.
  • Apply the “for” loop to iterate along the array elements in order to copy its contents and place them in a new array named “updatedArray” since this array accumulates the space for the new integer to be appended.
  • Finally, transform the contents of the new array into a string via the “toString()” method and display it.

Output

In the above output, it can be seen that the specified integer is appended to the new array.

Approach 3: Append to an Array in Java Using “Indexing” Technique and “for” Loop

These approaches can be applied in combination to append a string to the array by applying a condition upon the array index.

Example
The following illustration clarifies the discussed concept:

String givenArray[] = {"Harry","David","Tom"};
int n = givenArray.length;
int index = 3;
System.out.println("The given array is: "+Arrays.toString(givenArray));
String newArray[] = new String[n+1];
int j = 0;
for(int i = 0; i<newArray.length; i++) {
if(i==index) {
newArray[i] = "Jacob";
}
else {
newArray[i] = givenArray[j];
    j++;
}}
System.out.println("The new array becomes: "+Arrays.toString(newArray));

In the above lines of code:

  • Likewise, recall the discussed methodologies to declare an array(string in this case) and determine its length.
  • In the next step, assign the index where the string needs to be appended and transform the provided array into a string via the “toString()” method.
  • After that, likewise, allocate the size of the new array, iterate through this array, and considering the “index” condition, place the stated string value at that position.
  • Finally, display the new array after converting its values into the string format.

Output

The above output signifies that an additional string is appended to a new array keeping the former specified values intact.

Conclusion

To append to an array in Java, apply the “toString()” method combined with the “ArrayList” class and “add()” method, “length” attribute, and “for” loop, or “Indexing” technique. These approaches can be utilized to append an entry with the help of a list and append an integer or a string into a new array by specifying its(array) length, respectively. This blog elaborated on the approaches to append to an array in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.