Skip to content

Java Array Size | Resize Array, Java Array Without Size with examples

  • by

The Array Object is storing the same kind of data. Mostly in Java Array, we do searching or sorting, etc. For that, we do use a loop needs like Java for loop, so in the loop, we need to know Java array size for a number of iteration.

This Tutorial will show how to get the Java Array Size with some examples.

Java Array Size Resize Array, Java Array Without Size with examples

Syntax

The syntax of Java Array has a property declared as in document. So getting a size of Array is final property.

public final length;
int arrayLength = testArray.length;

Java array size Example 

Java array length change can’t change after creation and initialization. So the value of the length property does not change in the lifetime of the array object.

Here is a simple example of how to use a Java Array’s Size.

public class JavaArraySize {

    public static void main(String[] args) {
        String[] testArray = {"Cricket", "Football", "Volleyball"};
        int arrayLength = testArray.length;
        System.out.println("The size of the array is: " + arrayLength);
    }
}

Output: The size of the array is: 3

Another example

An upper example was with string array now do with an int Array and for a loop.

public class JavaArraySize {

    public static void main(String[] args) {
        int[] array = {9, 0, 9, 8};
        int arrayLength = array.length;
        System.out.println("Array Size is: " + arrayLength);

        //run loop and print elements
        for (int i = arrayLength; i > 0; i--) {
            System.out.println("Now remain Loops " + i);
        }
    }
}

Output: Array Size is: 4
Now remain Loops 4
Now remain Loops 3
Now remain Loops 2
Now remain Loops 1

Array without a Size

How you will Declare an array in java without size?

You can do it with an ArrayList, It’s a collection framework used in Java that serves as dynamic data.

ArrayList<Integer> array = new ArrayList<Intger>();

Here is an example code Java Array without a size. For that, you need an import java.util.ArrayList; and get the size of ArrayList to need to use size() Method.

import java.util.ArrayList;

public class JavaArraySize {

    public static void main(String[] args) {
        ArrayList<Integer> array = new ArrayList<>();
        array.add(1);
        array.add(2);
        array.add(3);
        System.out.println("Size " + array.size());
    }
}

Output: Size 3

Q: Where we need the Java Array Size and How to use it.

Answer: Searching or sorting or print all elements of Array.

Let’s take an example we want a print all elements of the array, so that time we need to know the size of an array. It will give input to for loop how many times we need to loop run.

There is not a Java array size method, It’s a length property as mentioned in the above examples.

Q: How do you resize an array in Java?

Answer: You can’t Resize Array Java, But there is a way to do it:

  • Create a new Java Array with a new size (upgraded) and copy all array element to in new Array, using java.lang.System.arraycopy(...);.
  • A copy of the Array, using java.util.Arrays.copyOf(...) method. This method returns a bigger size array, with all elements of the original array.
  • The last one Use an ArrayList class, It’s resizable.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java Array Length Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *