×

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

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double min (double a, double...

2 minutes read.

Traverse through HashMap in Java

In this tutorial, we will discuss traversing through HashMap in Java Introduction: HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K,...

4 minutes read.

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.

Split String into String Array in Java

The String split() technique returns a variety of divided strings after the strategy parts the given String around matches of a given normal articulation containing the delimiters. The ordinary articulation...

4 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

Java While Loop

A while loop is used to repeatedly execute a set of statements as long as its condition evaluates to true. This loop checks the condition before it starts the execution...

1 minute read.

Java inheritance with Example

Java inheritance Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes...

7 minutes read.

How to add 6 Months to the Current Date in Java?

In this tutorial, we will learn how to add 6 months to the local or current date in Java language. We will begin our topic with basic concepts and would...

3 minutes read.

How to Convert String to char in Java

How to Convert String to char in Java There are two methods to convert String to char are: Using charAt() method Using tocharArray() method Using charAt() method This is the method of String class that...

3 minutes read.

Star Pattern Programs in Java

Star Pattern Programs in Java The star pattern programs in Java is the part of pattern programs in Java, which we discussed earlier. Right Triangle Star Pattern Filename: StarPatternExample.java public class StarPatternExample {              public static void...

4 minutes read.

Sublime Number in Java

In this tutorial, we will understand what is meant by sublime number in java. From the point of the coding interview, it is one of the important topics. We will we will...

4 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 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 String subSequence() method

This method returns a new character sequence i.e. subsequence of current sequence Syntax: public CharSequence subSequence(int beginIndex, int endIndex) Parameter: beginIndex ? begin index, inclusive. endIndex ? end index, exclusive. Return: specified subsequnce Throws: It throws IndexOutOfBoundsException...

1 minute read.

Java Set to List

In this article, you will be acknowledged about how the process of conversion from Set or HashSet to LinkedList happens and what are the possible ways involved in conversion process. First...

4 minutes read.

Java Get File Size

There are three ways to get the file size in Java. Using Java InputOutput Using NewInputOutput Library Files.size() Method FileChannel.size() Method Using Apache Commons InputOutput Using Java InputOutput: The Java IO package offers the classes that deal...

5 minutes read.

Java Math IEEEremainder() Method

The IEEEremainder() method of Math class calculates the remainder as prescribed by the IEEE754 standard. This method simply returns the remainder when f1 (dividend) is divided by f2 (divisor). Syntax: public static...

2 minutes read.

Java Integer max() method

The max() method of Integer class returns the greater of two int values. It returns the same result as by calling Math.max. Syntax public static int max(int a, int b) Parameters The parameters ‘a’...

2 minutes read.