Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Arrays in Java - Introduction and Practical Examples

In this article you'll see that using variables makes your programs flexible. Thanks to variables, you can conveniently store data in your programs and retrieve it by name. You can also get input from your program's user. The best thing about variables is that they can constantly change value.

An Introduction to Arrays

Often in your programs, you'll want to store many values that are related in some way. Suppose you manage a bowling league, and you want to keep track of each player's average. One way to do this is to give each player a variable in your program, as shown in Listing 1.

Listing 1 - Applet1.java: Using Variables to Track Scores.

import java.awt.*;

import java.applet.*;

public class Applet1 extends Applet

{

TextField textField1, textField2, textField3;

int avg1, avg2, avg3;

public void init()

{

      textField1 = new TextField(5);

      textField2 = new TextField(5);

      textField3 = new TextField(5);

      add(textField1);

      add(textField2);

      add(textField3);

      textField1.setText("0");

      textField2.setText("0");

      textField3.setText("0");

}

public void paint(Graphics g)

{

      g.drawString("Your bowlers' averages are: ", 50, 80);

      String s = textField1.getText();

      g.drawString(s, 75, 110);

      avg1 = Integer.parseInt(s);

      s = textField2.getText();

      g.drawString(s, 75, 125);

      avg2 = Integer.parseInt(s);

      s = textField3.getText();

      g.drawString(s, 75, 140);

      avg3 = Integer.parseInt(s);

}

public boolean action(Event event, Object arg)

{

      repaint();

      return true;

}

}

When you run Applet1, you can enter bowling scores into the three boxes at the top of the applet's display area. After you enter these averages, they're displayed on-screen as well as copied into the three variables avg1, avg2, and avg3.

Nothing too tricky going on here, right?

Now examine the listing. If you could find some way to make a loop out of this code, you could shorten the program significantly. How about a for loop that counts from 1 to 3?

But how can you use a loop when you're stuck with three different variables? The answer is an array. An array is a variable that can hold more than one value. As you know, a variable is like a box in memory that holds a single value. Now, if you take a bunch of these boxes and put them together, what do you have? You have an array. For example, to store the bowling averages for your three bowlers, you'd need an array that can hold three values. You could call this array avg. You can even create an array for a set of objects like the TextField objects Applet1 uses to get bowling scores from the user. You could call this array textField.

Now you have an array called avg that can hold three bowling averages and an array called textField that can hold three TextField objects. But how can you retrieve each individual average or object from the array? You do this by adding something called a subscript to the array's name. A subscript (also called an index) is a number that identifies the element of an array in which a value is stored. For example, to refer to the first average in your avg array, you'd write avg[0]. The subscript is the number in square brackets. In this case, you're referring to the first average in the array (array subscripts always start from zero.) To refer to the second average, you'd write avg[1]. The third average is avg[2].

 In this case, the three bowling averages are 145, 192, and 160. The value of avg[0] is 145, the value of avg[1] is 192, and the value of avg[2] is 160.

Example: Creating an Array

Suppose that you need an array that can hold 30 floating-point numbers. First, you'd declare the array like this:

float numbers[];

Another way to declare the array is to move the square brackets to after the data type, like this:

float[] numbers;

After declaring the array, you need to create it in memory. Java lets you create arrays only using the new operator, like this:

numbers = new float[30];

The last step is to initialize the array, a task that you might perform using a for loop:

for (int x=0; x<30; ++x)

      numbers[x] = (float)x;

These lines of Java source code initialize the numbers[] array to the numbers 0.0 to 29.0. Notice how the loop only goes up to 29. This is because, although there are 30 elements in the numbers[] array, those elements are indexed starting with 0, rather than 1. That is, the subscript is always one less than the number of the element you're accessing. The first element has a subscript of 0, the second a subscript of 1, the third a subscript of 2, and so on.

Example: Using a Variable as a Subscript

As you know, most numerical literals in a Java program can be replaced by numerical variables. Suppose you were to use the variable x as the subscript for the array avg[]. Then  if the value of x is 1, the value of avg[x] is 192. If the value of x is 3, the value of avg[x] is 160.

Now take one last, gigantic, intuitive leap (c'mon, you can do it) and think about using your subscript variable x as both the control variable in a for loop and the subscript for the avg[] and textField arrays. If you use a for loop that counts from 0 to 2, you can handle all three averages with much less code than in the original program. Listing 2 shows how this is done.

Listing 2 - Applet2.java: Using Arrays.

import java.awt.*;

import java.applet.*;

public class Applet2 extends Applet

{

TextField textField[];

int avg[]; 

public void init()

{

      textField = new TextField[3];

      avg = new int[3];

      for (int x=0; x<3; ++x)

      {

            textField[x] = new TextField(5);

            add(textField[x]);

            textField[x].setText("0");

      }

}

public void paint(Graphics g)

{

      g.drawString("Your bowlers' averages are: ", 50, 80);

      for (int x=0; x<3; ++x)

      {

            String s = textField[x].getText();

            g.drawString(s, 75, 110 + x*15);

            avg[x] = Integer.parseInt(s);

      }

}

public boolean action(Event event, Object arg)

{

      repaint();

      return true;

}

}

Line by line

- Tell Java that the program uses classes in the awt package.

- Tell Java that the program uses classes in the applet package.

- Derive the Applet2 class from Java's Applet class.

- Declare TextField and int arrays.

- Override the Applet class's init() method.

- Create the textField and int arrays with three elements each.

- Loop from 0 to 2.

- Create a new TextField object and store it in the array.

- Add the new TextField object to the applet.

- Set the new TextField object's text.

- Override the Applet class's paint() method.

- Display a line of text.

- Loop from 0 to 2.

- Get the text from the currently indexed TextField object.

- Draw the retrieve text on the applet's display area.

- Convert the value and store it in the integer array.

- Override the Applet object's action() method.

- Force Java to redraw the applet's display area.

- Tell Java everything went okay.

 

At the beginning of Listing2, you'll see a couple of strange new variable declarations that look like this:

        TextField textField[];

        int avg[];

These declarations are much like other declarations you've seen, except both of the variable names end with a set of square brackets. The square brackets tell Java that you're declaring arrays rather than conventional variables.

Once you have the arrays declared, you must create them. In Applet2, this is done like this:

        textField = new TextField[3];

        avg = new int[3];

Here you use the new operator to create the arrays. To tell Java the type of arrays to create, you follow new with the data type and the size of the array in square brackets. In other words, the first line above creates an array that can hold three TextField objects. The second line creates an array that can hold three integers.

Once you have your arrays created, you can use a loop to reduce the amount of code needed to initialize the arrays. For example, the long way to initialize the arrays (without using a loop) would look something like Listing 3:

Listing 3: Initializing an Array without Looping.

textField[0] = new TextField(5);

add(textField[0]);

textField[0].setText("0");

textField[1] = new TextField(5);

add(textField[1]);

textField[1].setText("0");

textField[2] = new TextField(5);

add(textField[2]);

textField[2].setText("0");

As you learned, however, you can use a variable-specifically, a loop control variable-as the array subscript. That's what Applet2 does, which enables it to initialize the textField array as shown in Listing 4.

Listing 4: Initializing an Array Using a Loop.

for (int x=0; x<3; ++x)

{

        textField[x] = new TextField(5);

        add(textField[x]);

        textField[x].setText("0");

}

The first time through the loop, x is equal to 0, so that element 0 (the first element) of the textField array is being manipulated. The next time through the loop, x is 1, so that element 1 of the array is being manipulated in the body of the loop. Finally, when x is 2, the program takes care of the third array element. As you can see, using a loop with an array can greatly simplify handling a group of related values. Imagine how many lines of source code you'd save if the array had 1,000 elements instead of only three. To accommodate the larger array, you'd only have to change x<3 to x<1000 in the first line of the for loop.

 

CAUTION

Be careful not to try accessing a nonexistent array element. For example, in Listing 4, if you tried to access textField[3], you'd be beyond the boundaries of the array. Java will generate an exception when this happens, which means your applet may or may not perform the way you want it to.

 

The init() method isn't the only place Applet2 takes advantage of a loop to handle the program's arrays. In the paint() method, you can see the loop shown in Listing 5.

Listing 5: The for Loop from the paint( ) Method.

for (int x=0; x<3; ++x)

{

        String s = textField[x].getText();

        g.drawString(s, 75, 110 + x*15);

        avg[x] = Integer.parseInt(s);

}

This loop simplifies the printing of the bowlers' scores and the loading of the avg[] array with the scores. Again, imagine how much time and space you'd save if the arrays in question had thousands of elements rather than only three. It's at times like those that you really learn to appreciate arrays.

NOTE

The memory locations that make up an array are called elements of the array. For example, in an array named numbers[], numbers[0] is the first element of the array, numbers[1] is the second element, and so on. The reason numbers[0] is the first element of the array is because of the number 0 inside the subscript.It is the number inside the subscript that defines which array location is being referred to.

 

Multidimensional Arrays

So far, you've looked at simple arrays that hold their data in a list. However, most programming languages also support multidimensional arrays, which are more like tables than lists. The first array is a one-dimensional array, which is like the arrays you've used so far in this article. The next type of array is two-dimensional, which works like the typical spreadsheet type of table you're used to seeing.

Although Java doesn't support multidimensional arrays in the conventional sense, it does enable you to create arrays of arrays, which amount to the same thing. For example, to create a two-dimensional array of integers, you might use a line of code like this:

        int table[][] = new int[4][4];

This line of Java code creates a table that can store 16 values-four across and four down. The first subscript selects the column and the second selects the row. To initialize such an array with values, you might use the lines shown in Listing 6.

Listing 6: Initializing a Two-Dimensional Array.

table[0][0] = 0;

table[1][0] = 1;

table[2][0] = 2;

table[3][0] = 3;

table[0][1] = 4;

table[1][1] = 5;

table[2][1] = 6;

table[3][1] = 7;

table[0][2] = 8;

table[1][2] = 9;

table[2][2] = 10;

table[3][2] = 11;

table[0][3] = 12;

table[1][3] = 13;

table[2][3] = 14;

table[3][3] = 15;

You refer to a value stored in a two-dimensional array by using subscripts for both the column and row in which the value you want is stored. For example, to retrieve the value 11 from the table[][] array, you use a line like this:

        int value = table[3][2];

A quick way to initialize a two-dimensional array is to use nested for loops, as shown in Listing 7.

Listing 7: Using Loops to Initialize a Two-Dimensional Array.

for (int x=0; x<3; ++x)

{

        for (int y=0; y<3; ++y)

        {

                table[x][y] = 5;

        }

}

If you've never seen nested loops before, you're about to discover how handy they can be. In the case of Listing 7, the outside loop (the x loop) starts first, setting x to 0. But the body of the loop is another loop. So the inside loop (the y loop) starts, setting y to 0, which brings the program to the line that initializes an element of the array. Because x and y both equal 0, the array element table[0][0] gets set to 5. Then the inside loop sets y to 1, which means table[0][1] gets set to 5. When the inner loop finishes, the program branches back to the outer loop, setting x to 1. The inner loop repeats again, only this time with x equal to 1 and y going from 0 to 2. Finally, when both loops finish, the entire array is initialized.

Of course, to create the array, you have to be a little more tricky, as shown in Listing 8. Work through each loop to see how the array gets initialized.

Listing 8: Initializing the Array Elements to Different Values.

for (int x=0; x<3; ++x)

{

        for (int y=0; y<3; ++y)

        {

                table[x][y] = x + y * 4;

        }

}

Example: Creating a Two-Dimensional Array

Suppose that you need a table-like array that can hold 80 integers in eight columns and 10 rows. First, you'd declare the array like this:

        int numbers[][];

After declaring the array, you need to create it in memory, like this:

        numbers = new int[8][10];

The last step is to initialize the array, probably using nested for loops:

        for (int x=0; x<8; ++x)

        for (int y=0; y<10; ++y)

        numbers[x][y] = 0;

These lines initialize the numbers[][] array to all zeroes.

Example: Using Two-Dimensional Arrays in an Applet

To be sure you understand how arrays work, you'll put a two-dimensional array to work in a program called Applet3. The Applet3 applet creates and initializes a two-dimensional array with six columns and eight rows. (Try to imagine the elements of this array as the rows and columns of a spreadsheet.) The program then prints the contents of the array in the Applet's display area, so you can see that the array truly holds the values to which it was initialized.

Listing 9 - Applet3.java: Using a Two-Dimensional Array.

import java.awt.*;

import java.applet.*;

public class Applet3 extends Applet

{

      int table[][];

      public void init()

      {

            table = new int[6][8];

            for (int x=0; x<6; ++x)

            for (int y=0; y<8; ++y)

            table[x][y] = x+y*6;

      }

      public void paint(Graphics g)

      {

            for (int x=0; x<6; ++x)

            for (int y=0; y<8; ++y)

            {

                  String s = String.valueOf(table[x][y]);

                  g.drawString(s, 50+x*25, 50+y*15);

            }

      }

}

Line by line

- Tell Java that the program uses classes in the awt package.

- Tell Java that the program uses classes in the applet package.

- Derive the Applet3 class from Java's Applet class.

- Declare a two-dimensional integer array.

- Override the Applet class's init() method.

- Create an array with six columns and eight rows.

- Loop from 0 to 5.

- Loop from 0 to 7.

- Initialize the currently indexed array element.

- Override the Applet class's paint() method.

- Loop from 0 to 5.

- Loop from 0 to 7.

- Convert the array element to a string.

- Display the array element's value.

 

Notice in init() and paint() how the nested loops don't have curly braces like the example shown in Listing 8. This is because when you have only one statement in a program block, the curly braces are optional. In Applet3's init() method, the outside loop contains only one statement, which is the inner for loop. The inner for loop also contains only a single statement, which is the line that initializes the currently indexed element of the array. In the paint() method, the outer loop contains only one statement, which is the inner for loop. However, the inner loop contains two statements, so the curly braces are required in order to mark off that program block.

So, is this folks, up to the next article, see ya!



MrBool is a totally free website made for software developers. Our articles and courses are totally free with focus on useful techniques and best practices

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login