Copyright

What is an Array in Java?

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Another complicated-sounding programming concept that is straightforward and powerful: The array is a sequence or list of values. In this lesson we will define Java arrays and provide real-world examples of their use.

Arrays

An array in Java is not an impossibly difficult concept, although many can be confused since it does sound like a very technical term. Rather, an array is simply a sequence of values, where each item is the same data type. An array is a single object, even though it's made up of any number of pieces.

Arrays can be used to take an entire string of characters, such as a user name, and chunk it out into single characters. Yet to Java, the entire block of text is still a single unit. It is like accessing a cell within a table: you can always reference cell 4 without having to disassemble the entire table.

Let's look at a basic array and how it is defined in Java.

Basic Array

int[] sequence;
sequence = new int[50];

In order to better understand the concept, and see the array truly as a sequence (or a one-sided table), let's create a new array and fill it with only a few values:

int[] series = new int[5];
series[0] = 22;
series[1] = 44;
series[2] = 96;
series[3] = 122;
seriex[4] = 999;

Or, another way to do this is:

int[] series = {22, 44, 96, 122, 999};

We've given each bucket a value: the following diagram shows a visual representation of what this looks like:


Java Array Bucket


You'll notice one thing right away: the list starts at zero.

Everything Starts at Zero!

Remember that Java starts counting at ZERO, not 1. Therefore the first bucket in any array is 0, e.g., myArray[0]; the second ins myArray[1], etc. This can be a little confusing, but it is the standard practice in Java, and many other programming languages.

Length is Fixed

After the array is built you can't make it longer or shorter. However, you can always get the length of an array by using the length function. The following shows how to get the length of our sample array (it is embedded in the print routine):

int[] sequence;
sequence = new int[50];
System.out.println(sequence.length);

And the output:


Java Array Length Output


The most common way to run through an array is via the for loop: the loop lets you step through the array 1 by 1. Since we already know the length, it's fairly straightforward to step through each block in the sequence and display it. In Java, our counters are usually the letter i, an integer value. Here is a simple example of stepping through an array:

for(int i = 0; i < myArray.length; i++) {
 System.out.println(myArray[i]);
 //print out each block in the sequence
}

Multiple Dimensions!

So far we've only discussed arrays that are one-dimensional, that is a true sequence. Java supports arrays with any number of dimensions, although 2-dimensional are the most common (and easiest to understand). A two-dimensional array is like a table, with rows and columns.

The following example shows a very basic creation of a table-like array: It will have 10 rows and only 3 columns.

int[][] myMatrix;
myMatrix = new int[10][3];

Looping through 2-dimensional arrays requires a second for loop, within the first. Additional dimensions in Java arrays can get very complicated very fast, so let's just stick to the basics for now. Remember that a one-dimensional array is declared with one set of brackets ([]); a 2-dimensional array with two ([] []), and so on.

Examples

Let's take a look at some examples that will help solidify the concept of the Java array.

Prefilling and Displaying the Array

The following example shows how an array is created, two methods for filling the arrays, and the output after looping through to show the values.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support