Add Elements to Array in Java27 Mar 2025 | 4 min read In Java, arrays are basic data structures for storing elements of the same type in consecutive memory locations. Although arrays have a fixed size once they are created, there are different ways to add elements or create new arrays with new elements In this section, we will explore different ways adding elements to an array in Java. An array is a collection of identical objects stored in consecutive locations in memory. The main advantage of an array is that we can randomly access the items in an array, whereas the elements in a linked list cannot be randomly moved. In Java, Arrays are mutable data types, i.e., the size of the array is fixed, and we cannot directly add a new element in Array. However, there are various ways to add elements to the array. Using ArrayListJava's ArrayList is a dynamic array implementation that generates resizable arrays. It belongs to the Java Collections Framework and provides ways to add objects dynamically. Here's how we can use ArrayList to add elements to an array-like structure: File Name: UsingArrayList.java Output: 10 20 30 Using Arrays.copyOf()The arrays.copyOf() method in Java allows us to copy an existing array into a new array of specified length. We can add elements to the original array and assign additional lengths. File Name: UsingArrayscopyOf.java Output: 1 2 3 4 5 6 7 8 Using System.arraycopy() MethodThe Java System.arraycopy() method is used to copy data from one array to another. We can use this method to create new layouts with additional features. File Name: Systemarraycopy.java Output: 1 2 3 4 5 6 7 8 Shifting elements to adjust the size of the arrayIn this method, we will add the elements to the specified index in the array. Likewise, the above two processes will use a new destination array with a larger size than the original array. However, it will be tricky to shift the destination array elements after copying all elements from the original array to destination array. We will follow the steps given below to add element in the array:
Consider the following example in which we will add a specific value at the given index 3 in the original array using a destination array. File Name: JavaAddElementArraySpecified.java Output: Original Array: [1, 2, 3, 4, 5, 6] Array after adding value: [1, 2, 3, 7, 4, 5, 6] ConclusionIn Java, elements can be added to an array using various methods such as ArrayList, Arrays.copyOf(), and System.arraycopy(). The advantages of each option depend on the specific requirements of your application. Understanding these techniques allows us to manipulate arrays in Java programs. |
In Java, Vaadin Framework is an open-source framework to develop web applications. We can work with JavaScript and AJAX because it comes with support for both of them. By using Google Web Toolkit, we can include the external feature in it. Vaadin framework renders rich data...
5 min read
Problem Statement Given a number n representing n coins, we need to form a staircase with these coins. The i-th row of the staircase contains exactly i coins. The goal is to determine the total number of complete rows that can be formed with n coins. Approach...
5 min read
A concrete subclass of the Calendar class is referred to as GregorianCalendar. The GregorianCalendar class has an implementation of all of its inherited members. The Calendar class implements the mostly used Gregorian calendar. In order to use the Gregorian calendar in Java, we import the Java.util.GregorianCalendar...
16 min read
In this section, we will learn what is a nonagonal number and also create Java programs to check if the given number is a nonagonal number or not. The nonagonal number program is frequently asked in Java coding interviews and academics. Nonagonal Number Nonagonal numbers are the figurate...
5 min read
The Word Break Problem is about deciding whether a specific string can be divided into valid words that are present in a given dictionary. The objective is to determine if the string can be segmented into one or more words from this list. This problem can...
16 min read
The use of recursion to reverse a doubly linked list in Java requires an understanding of both the structure of a doubly linked list and the recursion process. A doubly linked list's nodes are composed of three components: a data field, a pointer to the node...
5 min read
In traditional binary trees, traversal requires recursion or a stack-based approach to keep track of nodes. However, these methods introduce additional space complexity. Threaded Binary Trees simplify traversals through NULL pointer implementation that connects nodes to their immediate in-order predecessors or successors without requiring additional memory...
7 min read
Java provides a number of method calls to check and change the permission of a file, such as changing a read-only file to have permissions to write. File permissions are required to be changed when the user wants to restrict or modify the operations permissible...
5 min read
In programming, converting one type to/ from a type is a crucial task. Sometimes we require, conversion from one type to another type. In the Java conversion section, we have discoed various types of conversions. In this section, we can discuss how to convert hexadecimal into...
7 min read
Swapping corner words and reversing middle characters in Java exemplify a creative approach to string manipulation, a fundamental programming aspect. The task involves altering the positions of the first and last words in a string while inverting the order of the characters nestled between them. Example 1: Input:...
8 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India