×

How to add Elements in Array in Java

Consider an array of the size n, in the given size we must add the elements in the given array.

In this tutorial we are preferred to learn how to add the elements to array in the java.

Basically, the size of the array cannot be changed in the java using static way or dynamic way. But in the java the array can be created in the dynamic way.

As per the array definition:

Array is the collection of homogenous or the similar data elements stored in a single variable name under single data type is known as Array.

Note: All the collection of elements in java will be spell it as objects or collection frame work.

In java arrays will be stored as the dynamic way under the heap area in JVM (java virtual machine). In java arrays there is no concept of storing the elements in a static way.

The main benefit of an array that can be accessed randomly but in the linked list we cannot be accessed elements directly.

Arrays in the java that are mutable data types. The size of the array in the java is fixed and, in that array, we cannot add the new elements. We have the various methods to add the elements in the array.

The following methods we used to add the elements to the array are:

  • Creating the bigger array size than the current array size.
  • Using the ArrayList

1.  Creating the bigger array size than the current array size

For adding the elements in the java, we must create another bigger size array and copy the elements from the previous array and paste the elements into the bigger size array and place the new element to the newly created array.

We cannot add the elements directly into the

Let us see an example for adding the elements in the java array:

Example 1:

import java.util.Arrays;
public class Example
{
public static void main (String [] args)// main method
{
int array[] = {18,22,35,46,57,66}; 
int n = array.length;    // length of the array
int myArr[] = new int[n+1];
int element = 70;
System.out.println(Arrays.toString(array));
for(int i = 0; i<n; i++)
{
myArr[i] = array[i];
}
myArr[n] = element;
System.out.println(Arrays.toString(myArr));
}
}

Output:

How to Add Elements in Array in Java

2. Using the ArrayList

As we know that the ArrayList is the collection Frame work which will be created in the dynamic way. We have change the ArrayList elements to the array using the to Array() method for adding the elements.

  1. Change the Array elements into ArrayList Method using asList() method for storing the elements.
  2. Enter the elements in to the array list for storing the elements using the add() method.
  3. Change the ArrayList elements into Array using the method called toArray() for storing the elements.

Let us see an example program for ArrayList for storing the elements.

Example 2:

import java.util.*;
class ArrayListDemo
{
public static void main(String args[])
{
ArrayList<String> a = new ArrayList<String>();
System.out.println("the size of the array list is =" + a.size());
System.out.println("the objects of the array list is =" + a);
a.add("APPLE");
a.add("BANANA");
System.out.println("the size of the array list is =" + a.size());
System.out.println("the objects of the array list is =" + a);
System.out.println("1-using for loop");
for (String i:a)
{
System.out.println(i);
}
System.out.println("2-using iterator");
Iterator it = a.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
a.add("MANGO");
a.add("PAPAYA");
System.out.println("the size of the array list is =" + a.size());
System.out.println("the objects of the array list is =" + a);
System.out.println("3-using list iterator forward");
ListIterator lit = a.listIterator();
while(lit.hasNext())
{
System.out.println(lit.next());
}
System.out.println("3-using list iterator backward");
while(lit.hasPrevious())
{
System.out.println(lit.previous());
}
System.out.println("the size of the array list is =" + a.size());
System.out.println("the objects of the array list is =" + a);
a.remove("APPLE");
System.out.println("the size of the array list is =" + a.size());
System.out.println("the objects of the array list is =" + a);
}
}

Output:

How to Add Elements in Array in Java

Related Topics

Catalan number in Java

In general mathematics, Catalan numbers can be defined as the sequence of natural numbers that frequently occur in counting problems often encountered in recursively defined objects. Mathematical formula of Catalan number Coming...

3 minutes read.

Lazy loading in Java

Lazy loading is the idea of waiting to load an object until you need it. In other words, it is the practice of postponing class instantiation until it is necessary....

5 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

Special Operators in Java

An operator in Java is a special symbol. It is used to perform operations on two or more variables.  We all know basic operations in Java. There are 8 types...

5 minutes read.

Client Server Program in Java

Client Server Program in Java The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is...

7 minutes read.

Nth Term of Geometric Progression in Java

There are 3 numbers provided. The geometric progression's initial term is the first number. The second number is the geometric progression's common ratio, and the third number represents the nth...

3 minutes read.

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers. Tetranacci number Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends...

3 minutes read.

Equidigital in Java

In this section, we will understand what is an equidigital number and how to write Java programs to locate them. It is commonly asked in academic settings and Java coding...

4 minutes read.

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.

Checked vs Unchecked Exceptions in Java

In this tutorial, we will discuss Java's checked and unchecked Exceptions. Exception An exception is an undesirable event that disrupts the normal flow of the program. An exception is thrown at runtime. It...

4 minutes read.

Java Macros

Macros are additions to the JDK 7 compiler in Java. Compile time macros are added and supported. Macros are Java classes that are instantiated and executed during the compilation process....

2 minutes read.

How to Iterate List in Java?

List is a Collection foundation interface in Java. It enables us to keep the collection of objects in order. The four classes that make up the List interface implementation are...

3 minutes read.

The Maximum Rectangular Area in a Histogram in Java

Continuous bars should be used to form the largest possible rectangle. We'll assume in the interest of convenience that each bar's width is 1. Naive Approach In this method, each bar will be...

6 minutes read.

Java Flags Enum

In a programming language, enumerations represent a group of named constants.For instance; the four suits in a deck of playing cards could be the enumerators Club, Diamond, Heart, and Spade,...

3 minutes read.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

All the important string methods in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

If Condition in Lambda Expression Java

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single-method interface using an expression....

4 minutes read.

Java Characters

Normally, when we work with characters, we use primitive data types char. When we have to work with the objects of char, we use Character class. Character class has many important...

2 minutes read.

Moran Numbers in Java

In this article, we will be acknowledged about the moran numbers in Java, how they are formed, what are the approaches to achieve the moran numbers. Moran Number A moran numbers are...

3 minutes read.