Skip to content
Fix Code Error

Array Length in Java

March 13, 2021 by Code Error
Posted By: Anonymous

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?

Solution

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

Answered By: Anonymous

Related Articles

  • How can I resolve Web Component Testing error?
  • Unable to run Robolectric and Espresso with a…
  • TLS 1.3 server socket with Java 11 and self-signed…
  • When I'm testing a web app by JUnit and Mockito I get many…
  • JUNIT @ParameterizedTest , Parameter resolution Exception
  • ClassNotFoundException:…
  • Eclipse will not start and I haven't changed anything
  • Neither BindingResult nor plain target object for bean name…
  • Creating an dynamic array, but getting segmentation fault as…
  • Jetpack Compose and Hilt Conflict

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

From an array of objects, extract value of a property as array

Next Post:

How can I open a URL in Android’s web browser from my application?

Leave a Reply Cancel reply

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

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error