DEV Community

loading...
Cover image for Arrays in Java.

Arrays in Java.

tarunj096 profile image Tarun Kumar J Originally published at tarunjain.hashnode.dev Updated on ・3 min read

Hello Everyone 💫!

So I mentioned I will write about Java in my previous article and I was thinking which topic should I choose, And I finally chose ** Data Structures ** in Java.

This article would help every beginner studying in Java and I hope this helps you understand Arrays in Java easily. Another reason being, It would help me get my understanding strong as well.
So let's start discussing Arrays in Java.

Arrays

An array is a data structure that stores elements of the same data type at contiguous(next to or adjacent) memory locations.

So lets first find out how to create an array.

The below code creates an array of 10 integers.

int[] numbers = new int[10];

To create an array to hold 100 names:-

String[] names = new String[100];

Another way of creating an array is by initiating it with values when you declare the array.
The code down creates an array with 5 integers and initiates it with values.

int[] number = { 8, 10, 26, 9, 96 };      // It creates an array of 5 integers.

We can also initialize an array of strings in the same way.

String[] names = { "Tarun", "Louis" , "John" };

In Java, the first index of an array starts with 0. So if the length of an array is 5 then the last element's index is 4.The elements are also called values.

Assigning and printing the elements

An element of an array is referred to by its index. In the below code we create an array of two numbers and assign values to the indices 0 and 1.

int[] numbers = new int[2];

numbers[0] = 18;
numbers[1] = 26;

System.out.println(numbers[0]);
System.out.println(numbers[1]);

NOTE: You can find the size of the array by length variable. We can get that by name of the array dot length, ie by ** numbers. length **
and If we print that It will give us the size of an array.

int[] numbers = new int[2];
System.out.println(numbers.length);   // It will print 2

If we write the index outside of an array ie bigger than the index size, We get an ArrayIndexOutOfBoundsException, ie It tells us that the array doesn't contain the given index.

String[] arrayOfNames = { "Hello" , "there"}; //The array contains only two elements ie index 0 and 1.

System.out.println(arrayOfNames[3]); //This will give us an ArrayIndexOutOfBoundsException.

Iterating over an Array

This means accessing each element of an array one by one. I'd help you understand that with an example below.

String[] names = new String[3];
names[0] = "Tarun";
names[1] = "Jonathan";
names[2] = "Vicky";

System.out.println("The length of the array is "+names.length+"."); 

//The below while loop prints every element of the array by iterating over it.
int index = 0;
while(index<names.length){
      System.out.println(names[index]);
      index = index + 1;
}

/* 

The Output of the program will be:
The length of the array is 3.
Tarun
Jonathan
Vicky

*/

The above code finds the length of the String array and prints it, And then it iterates over the array and prints out every string element one by one.

Finally, I'd like to say that If anyone wants to learn more in Java, There are free courses out there.You can choose whichever you like and start learning, The best ones I've found are :

Thanks for reading! Hope you like it 🙂.

See you in the next article until then, Happy Coding ⚡️!

PS:
If you find any mistakes in the above article/blog, Please comment it out, So that I would make the edits.

Discussion (0)

pic
Editor guide