[java] Array Length in Java

I declared an array as shown below:

int[] arr = new int[10];

Then I assigned following values to the array:

arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;

Then I declared and initialized an integer variable:

int arrayLength = arr.length;

This will be useful to find actual size but is there any method to find logical size of the array?

This question is related to java arrays

The answer is


It should be:

int a = arr.length;

Parenthesis should be avoided.


If you want the logical size of the array, you can traverse all the values in the array and check them against zero. Increment the value if it is not zero and that would be the logical size. Because array size is fixed, you do not have any inbuilt method, may be you should have a look at collections.


  int arr[]={4,7,2,21321,657,12321};
  int length =arr.length;
  System.out.println(length); // will return 6

  int arr2[] = new int[10];
   arr2[3] = 4;
  int length2 =arr2.length;
  System.out.println(length2); // // will return 10. all other digits will be 0 initialize because staic memory initialization

First of all, length is a property, so it would be arr.length instead of arr.length().

And it will return 10, the declared size. The elements that you do not declare explicitely are initialized with 0.


Arrays are allocated memory at compile time in java, so they are static, the elements not explicitly set or modified are set with the default values. You could use some code like this, though it is not recommended as it does not count default values, even if you explicitly initialize them that way, it is also likely to cause bugs down the line. As others said, when looking for the actual size, ".length" must be used instead of ".length()".

public int logicalArrayLength(type[] passedArray) {
    int count = 0;
    for (int i = 0; i < passedArray.length; i++) {
        if (passedArray[i] != defaultValue) {
            count++;
        }
    }
    return count;
}

In this case, arr.length will return 10, the size of array you allocated. Logical size doesn't really apply here, as this is a fixed length array.

When you initialize the array:

int[] arr = new int[10];

Java will create an array with 10 elements and initialize all of these to 0. See the Java language spec for details of initial values for this and other primitive types.


It contains the allocated size, 10. The remaining indexes will contain the default value which is 0.


It will contain the actual size of the array as that is what you initialized the array to be when you declared it. Java has no concept of the "logical" size of an array, as far as it is concerned the default value of 0 is just as logical as the values you have manually set.


Arrays are static memory allocation, so if you initialize an array of integers:

int[] intArray = new int[15];

The length will be always 15, no matter how many indexes are filled.

And another thing, when you intialize an array of integers, all the indexes will be filled with "0".


`

int array[]=new int[3]; array.length;

so here we have created an array with a memory space of 3... this is how it looks actually

0th 1st 2nd ...........> Index 2 4 5 ...........> Number

So as u see the size of this array is 3 but the index of array is only up to 2 since any array starts with 0th index.

second statement' output shall be 3 since the length of the array is 3... Please don't get confused between the index value and the length of the array....

cheers!


Java arrays are actually fixed in size, and the other answers explain how .length isn't really doing what you'd expect. I'd just like to add that given your question what you might want to be using is an ArrayList, that is an array that can grow and shrink:

https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

Here the .size() method will show you the number of elements in your list, and you can grow this as you add things.


int[] arr = new int[10];

arr is an int type array which has size 10. It is an array of 10 elements. If we don't initialize an array by default array elements contains default value. In case of int array default value is 0.

length is a property which is applicable for an array.
here arr.length will give 10.


if you mean by "logical size", the index of array, then simply int arrayLength = arr.length-1; since the the array index starts with "0", so the logical or "array index" will always be less than the actual size by "one".


In Java, your "actual" and "logical" size are the same. The run-time fills all array slots with default values upon allocation. So, your a contains 10.


To find length of an array A you should use the length property. It is as A.length, do not use A.length() its mainly used for size of string related objects.

enter image description here

The length property will always show the total allocated space to the array during initialization.

If you ever have any of these kind of problems the simple way is to run it. Happy Programming!