This tutorial focuses in how to determine the array size in java. Arrays are one of the most overused storage of information in java since the declaration is easy and a lot of API’s requires this collection as an input and also as an output. Basically it holds a list of information whether its only a strings or complicated lists of objects. Focus on this knowledge, since we would be using it thoroughly on our programming life. However, I prefer more to use ArrayList since it is more flexible and has a lot more functions that is necessary on my everyday programming. But don’t get me wrong, in order to understand the much more complicated API, we should master first all the important concepts of an array.
On this article we would be focusing on determining how large is our array is. This is very important to learn since we would be using it in our loops especially if the java codes we are maintaining are still not using the advance for loop. The size is necessary to determine the upper bound of the index we should be looping in. Moreover, the size of an array might also be necessary on your business requirements.
JAVA examples : API needed in getting the array size in java
- length – its an attribute of an array giving the current size
Java array example source code
package com.javatutorialhq.tutorial; /** * This java sample source code that shows * how to get the array size in java * Property of javatutorialhq.com * Feel free to modify for your own personal use * All Rights Reserved * Version 1.0 * 07/31/2013 */ public class ArraySize { public static void main(String[] args) { Integer[] arrayExample = new Integer[] {1,2,3,4,5,6,7}; //Printing the length of java array System.out.println("Array Size is: "+arrayExample.length); } }
Sample Output:
Array Size is: 7