1. Overview
In this tutorial, We'll be learning how to add new values to an array in java.
In java, once the array is created then it can not be resized. That means increasing or reducing the size in java is not possible.
You need to look for alternative ways to add to array in java.
Adding int or string to an array can be implemented in ways as below.
A) By recreating the new array
B) By intermediate storage such as ArrayList or HashSet if the array values are unique.
This works for any type of array such as adding to int array or string array.
2. Java add to array by recreating new array
First, create the new array with the same size as the existing array and increment it by 1.
Next, loop through the existing array and get each value. Now, add the value into the new array at the same index. At last, add new value to the last.
Look at the below program and the array is added with the new value.
package com.javaprogramto.arrays.add; import java.util.Arrays; public class JavaArrayToAddExample { public static void main(String[] args) { int[] values = new int[5]; values[0] = 0; values[1] = 1; values[2] = 2; values[3] = 3; values[4] = 4; int[] newArray = new int[5 + 1]; for (int i = 0; i < values.length; i++) { newArray[i] = values[i]; } int newValue = 5; int newArraylength = newArray.length; newArray[newArraylength - 1] = newValue; System.out.println("existing array values " + Arrays.toString(values)); System.out.println("new array values " + Arrays.toString(newArray)); } }
Output:
existing array values [0, 1, 2, 3, 4] new array values [0, 1, 2, 3, 4, 5]
3. Java add to array by using intermediate storage - ArrayList
In this approach, We'll use ArrayList as the temporary storage and convert arraylist back to array using toArray() method of List.
package com.javaprogramto.arrays.add; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class JavaArrayToAddExample2 { public static void main(String[] args) { Integer[] array = { 10, 20, 30, 40, 30, 40 }; System.out.println("Initial array values " + Arrays.toString(array)); List<Integer> integers = new ArrayList<>(); for (int a : array) { integers.add(a); } int newValue = 50; integers.add(newValue); array = integers.toArray(array); System.out.println("Array after adding 50 value " + Arrays.toString(array)); } }
Output:
Initial array values [10, 20, 30, 40, 30, 40] Array after adding 50 value [10, 20, 30, 40, 30, 40, 50]
4. Java add to array using HashSet
HashSet can be used in place of ArrayList to add values to the array.
But, you need to remember two things that if the array has duplicate values then the duplicate values are omitted from final array.
And an order of elements in the array is not gurrenteed.
So this way is not recommended unless the above two are needed to remove the duplicate values and its order.
import java.util.Set; public class JavaArrayToAddExample3 { public static void main(String[] args) { Integer[] array = { 10, 20, 30, 40, 30, 40 }; System.out.println("Initial array values " + Arrays.toString(array)); Set<Integer> integers = new HashSet<>(); for (int a : array) { integers.add(a); } int newValue = 50; integers.add(newValue); array = integers.toArray(array); System.out.println("Array after adding 50 value " + Arrays.toString(array)); } }
Output:
Initial array values [10, 20, 30, 40, 30, 40] Array after adding 50 value [50, 20, 40, 10, 30, null]
From the above output, you can observe that order and values are missing from the original array.
5. Conclusion
In this article, we've seen what are the different ways to add values to an array in java.
No comments:
Post a Comment
Please do not add any spam links in the comments section.