i

JAVA Complete Course

Single Dimension Array

We have already touched one arrays few chapters before. Let’s shortly say what is array. An array is a collection of elements of one specific type in a horizontal fashion. The array in contention here is that of the one-dimensional array in Java programming. 

There are few ways to declare single dimensional array:

And instantiation of array can be done with new keyword.

There is also way to declare and initialize array together at the same line:

As we have already mentioned that to traverse array we can use loop operations, let’s see an example of using different ways to print elements of array.

While working with arrays we can meet ArrayIndexOutOfBoundException. We will cover exceptions later but this happens when we try to access element of array on the index which doesn’t exists. For example, any index below 0 will cause this error or any index which is higher or equal the size of array. The first index of array is 0, and the last one is length of array - 1.