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:
1 |
arrayName.length |
Example: How to get the number of elements of the fixed length array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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:
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:
1 |
listName.size() |
Example: How to get the number of elements of the variable length ArrayList:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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:
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:
1 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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:
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:
1 2 3 |
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.