Array is one many data structures in programming. All the elements in the Array are stored in contiguous manner inside memory. This is how you can visualize array.
Each box represents one memory location. The number inside the box is the value that corresponding memory location holds.
Below the box, the numering that starts from 0(zero) are called index. Index is used to retrieve the array elements.
Array declaration is quite simple. This is how you can declare Array in Java.
int numbers[]; // declaring the array numbers = new int[100]; // allocating memory to array
Same can be done in single line.
int numbers[] = new int[100];
Note: Providing the array size at the time of creation is must.
Using array index we can store values inside array.
int numbers[] = new int[100]; // declaring and creating array of size 100 numbers[0] = 1; // storing value 1 at the index 0 numbers[1] = 12; numbers[2] = 5; numbers[3] = 6; numbers[4] = 7;
We have created and stored values similar to the Figure 1.1.
Array index is used to access the array elements.(I know, I said something too obvious and silly!)
System.out.println(numbers[0]); // prints => 1
In Java array is an object, so it inherits few methods and fields. Most important field that will be most of the time is length.
length: Java provides length field on array. This is how you can get the length of the array.
System.out.println(numbers.length); // prints => 100
Array is very simple and easy to use data structure. Along with that these are few more pros of using Array.
It has some downside too.
Hope you liked this tutorial. You might also like other tutorials in this series here
Quick Links
Legal Stuff