Java

Java ArrayCopy Method: How to add number of elements in an array in java using NetBeans

Description:

Java Arraycopy Method– in this article, you will learn to use the System class method arraycopy. There are two types of arrays: fixed-length arrays with a predetermined number of elements and variable-length arrays (lists) that can be changed later.

Syntax to get the number of elements:

The table summarizes how to get the number of elements for each.

Fixed Length Array Variable length List
Arrayname.length Listname.size()

I will explain how to use each.



Arrays with fixed length:

Use the length method of the java to get the number of elements of the fixed-length array.

Syntax of fixed length Array:

arrayName.length

Example: How to get the number of elements of the fixed length array:

package com.mycompany.javabasics;

/**
 *
 * @author Fawadkhan
 */
public class ArrayExamples {

    public static void main(String[] args) {
        int[] arr = new int[30];
        System.out.println(arr.length);
    }

} 

output:

how to get the number of elements of the fixed length array in java

ArrayList (variable length)

ArrayList is a variable length array (list) whose number of elements can be changed later.

For ArrayList, you can get the number of elements in the array by calling the size method of the java.


Syntax of the Variable length ArrayList:

listName.size()

Example: How to get the number of elements of the variable length ArrayList:

package com.mycompany.javabasics;

import java.util.ArrayList;

/**
 *
 * @author Fawadkhan
 */
public class ArrayExamples {

    public static void main(String[] args) {
        ArrayList<Integer> arrlist = new ArrayList<>();
        arrlist.add(1);
        arrlist.add(2);
        arrlist.add(3);
        arrlist.add(4);
        arrlist.add(5);
        arrlist.add(6);
        arrlist.add(7);

        System.out.println(arrlist.size());

    }

}

output:

how to get the number of elements of the variable length arraylist in java

adding elements in an array using the java arraycopy method:

Sometimes you want to change the number of elements in an array in java. I will explain the case of an array (fixed length) and ArrayList (variable length) respectively.

Java Arraycopy method adding Elements in Fixed length arrays:

A fixed-length array cannot be written in a simple way because once the number of elements is determined, it cannot be changed.

As shown in the below code example, use the arraycopy method to prepare an array with a larger number of elements and copy the values.

int [] orignalArr = new int [ 20 ]; // original array  

int [] newArray = new int [ 40 ]; // Array to prepare if you want to add 5 data  

Syntax Java Arraycopy Method:

System.arraycopy(Arg0, Arg1, Arg2,Arg3, Arg4);

Whereas:

Arg0 is the source array of the arraycopy method from where you want to copy.

Arg1 specifies from which index the source array you want to start copying.

Arg2 is the target array of the arraycopy method in which you want to store the copied elements from the source array.

Arg3 is the index in which you want to store the values from the source array into the target array.

Arg4 is how many elements you want to copy from the source array into the target array.


Example: how to Combine arrays with System.arraycopy in java

package com.mycompany.javabasics;

import java.util.ArrayList;
import java.util.Arrays;

/**
 *
 * @author Fawadkhan
 */
public class ArrayExamples {

    public static void main(String[] args) {
        int[] originalArray = {1, 2, 3};
        int[] newArray = {4, 5, 6};

        int copy = originalArray.length + newArray.length;

        int[] finalArray = new int[copy];

        // First, copy the array of originalArray from the beginning to finalArray
        System.arraycopy(originalArray, 0, finalArray, 0, originalArray.length);

        // Next, copy newArray to FinalArray (copy start position is originalArray.length)
        System.arraycopy(newArray, 0, finalArray, originalArray.length, newArray.length);

        // display array finalArray
        for (int i = 0; i < finalArray.length; i++) {
            System.out.print(finalArray[i] + ", ");
        }

    }
}

Output:

how to combine arrays with the help of arraycopy method in java

Variable Length ArrayList

Since ArrayList is a variable-length array, using methods such as add and remove will add and remove elements and change the number of elements.

To add an element, write:

List <Element type name> List name = new ArrayList <Element type name>();   

List name. add (element value);


summary

In this article, you learned how to get the number of elements in an array. You can get the number of elements by using the length method for fixed-length arrays and by using the size method for variable-length arrays ArrayList. You’ll be using these two frequently when working with arrays.

Fawad

Shahzada Fawad

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button