Copyright

Java: Initializing an Array

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.

Arrays in Java are powerful tools in which you can store multiple elements within one object. But how do you initialize them? This lesson provides an overview and code examples for the use of arrays.

Java Arrays

Think of a Java array as a table. The simplest array only has one column and many rows. All the boxes in the table hold the same type of data, whether it is an integer, character, or double.

Declaring and Initializing an Array

Let's say we have a Java program for a game, and we need to keep track of the high scores. A good solution to storing high scores is in an array. We only want to keep 5 high scores, so we will limit the array to 5 buckets. To declare and initialize the array, the following code can be used:

int[] highScores;
//declare the array
highScores = new int[5];
//set the size to 5

The previous code is perfectly valid, but there is a shorter way to declare and initialize the array using only one line of code. The following code displays an example of default array initialization in Java. It is a short and simple way to initialize the array without having to use additional lines of code. For numeric arrays the default value is 0.

int[] highScores = new int[5];

So far we've created an array for high scores, then told it that we're only holding five values. It's a one-column table with only 5 rows. Right now the value is 0 (actually, it's whatever is in memory). If you add a line of code to print the array, it would look like this:


Java array output


But, now we have to either fill the array with data, or refer to each bucket within the array. To reference a bucket in the array you need to provide its index. Remember, Java starts counting at zero! Therefore, the first bucket will have an index of 0.

Right now all buckets are zero. So, if we printed the following, the FOURTH bucket in highScores:

System.out.println(highScores[3]);

The output is:


Java array print output


Now that we've covered the general way to initialize an array, let's look at some more options.

More Initializing Options

There are a few options for initializing an array. For small arrays, we can just populate the data. Larger, more complex arrays require other code to fill the values. In our previous example, it's fairly easy to pre-fill some values for the high scores (although the players of our fictional game might not be happy about that!).

Another way is to enclose the values within curly brackets. In this case, we didn't use the default initialization because we are going to fill the values right away:

int[] highScores = {100, 150, 276, 600, 1000};

We don't need to tell the Java the size of the array. We provided five high scores, so the length of the array is five. (highScores[0] is 100, and so on)

Another option is to initialize the array first, as a 1-dimensional array of 5 buckets for high scores. Next, we fill those scores a little differently.

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