2 Ways : Append To An Array In Java
Maybe your like
Java Hungry
Java developers tutorials and coding.
2 ways : append to an Array in Java In this tutorial, I will be sharing different ways to append an element to an Array in Java. After the creation of the Array, its size can not be changed. The different ways to append an element to an Array are as follow: 1. Creating new Array 2. Using ArrayList Read Also: Array interview questions1. Creating new Array
We will create a newArray, greater than the size of the givenArray. In the below example we are adding a single element to the end of the Array. 1. If the size of the givenArray is n then the size of the newArray will be n+1. 2. Copy the elements from the givenArray to the newArray. We will be using Arrays.copyOf method. Note: Arrays.copyOf(givenArray, newSize) is used to create a new Array of size newSize. 3. Insert(add) the new element at the end of the Array. import java.util.*; public class JavaHungry { public static void main(String args[]) { // Initialize givenArray int[] num = {2,4,6,8,10}; // Print the givenArray System.out.println("Given array is : "+Arrays.toString(num)); /* Using Arrays.copyOf(int[],int) method to create an Array of new size */ num = Arrays.copyOf(num, num.length+1); // Calculate the new length of the array int length = num.length; /* Store the new element at the end of the Array */ num[length - 1] = 12; // Print the new Array System.out.println("New array is : "+Arrays.toString(num)); } } Output: Given array is : [2, 4, 6, 8, 10]New array is : [2, 4, 6, 8, 10, 12]2. Using ArrayList
1. Create an ArrayList from the givenArray by using the asList() method. 2. Add the element to the ArrayList using the add() method. 3. Convert the ArrayList to Array using the toArray() method. import java.util.*; public class JavaHungry { public static void main(String args[]) { // Initialize givenArray Integer[] num = {3,6,9,12,15}; // Print the givenArray System.out.println("Given array is : "+Arrays.toString(num)); /* Convert Array to ArrayList using Arrays.asList() method */ List<Integer> arrlistObj = new ArrayList<Integer>(Arrays.asList(num)); // Add new element to the ArrayList arrlistObj.add(18); // Convert the ArrayList to Array num = arrlistObj.toArray(num); // Print the new Array System.out.println("New array is : "+Arrays.toString(num)); } } Output: Given array is : [3, 6, 9, 12, 15]New array is : [3, 6, 9, 12, 15, 18] That's all for today, please mention in comments in case you have any questions related to append an element to an Array in java. Reference: Arrays.copyOf Java docAbout The Author Subham Mittal has worked in Oracle for 3 years. Enjoyed this post? Never miss out on future posts by subscribing JavaHungry
Get new posts by email:
SubscribeWHATS HOT
- Best Laptops for Programming
- Best Books for Learning Java
- Amazon Interview Question : First Non repeated character in String
- Count total number of times each alphabet appears in the string java program code with example
- Java 8 new features : Lambda expressions , optional class , Defender methods with examples
POPULAR POSTS
- Java Interview Questions
- Top 50 Java Collections Interview Questions and Answers
- Web Services Interview Questions
- Java Multithreading Interview Questions and Answers
- Java Interview Programs
Tag » How To Append An Array Java
-
How To Append To An Array In Java
-
How To Add New Elements To An Array? - Java - Stack Overflow
-
Java - Append To Array - Tutorial Kart
-
How To Add An Element To An Array In Java? - GeeksforGeeks
-
Add Elements To Array In Java - Javatpoint
-
Java: Appending To An Array - Programming.Guide
-
Adding An Element To A Java Array Vs An ArrayList - Baeldung
-
How To Add Elements To An Array In Java - Software Testing Help
-
How To Append A Number Into A Java Array - Quora
-
How To Append An Array In Java - Know Program
-
Adding Elements To An Array In Java - YouTube
-
How To Append An Element To An Array In JavaScript? Example Tutorial
-
Append One Array To Another : Array Insert Remove
-
Append To Array | Learning Processing 2nd Edition