Papa’s Got a Brand New Bag: Using Arrays to Store Data

12.01.2018 |

Episode #8 of the course An introduction to Java programming by Alex Theedom

 

Welcome to the eighth of ten lessons about the Java programming language. You are almost at the end of the course and well on your way to being a Java programmer. Let’s get coding. Today, you are going to learn how to store data in an array data structure.

 

Step 1: Preparation

• Go to the online IDE, codiva.io, sign in, and click the New Project button.

• Call today’s project, Lesson 8.

• Create a class file called, ShoppingCart.

• Delete the line System.out.println(“Hello Codiva”);.

Now you are ready to start coding.

 

Step 2: Start Coding

Remember that in a previous lesson, you used a variable to store a single number or a password? Well, there are data structures that allow you to store a collection of related things in one variable. This is called an array.

An array is like a list. It stores numbers or words that are related to each other in some way. For example:

• If you are writing a bookshop website, you could store a list of all the books that your website sells in an array.

• If your site is an online shop, you could use an array to store all the items in the customer’s shopping cart.

• And so on.

An array is declared and initialized with a maximum size. This is the maximum number of things (number, words, etc.) you can store in the array. An array is also declared with a type (String, int, float, etc.) so the same type of data is stored.

The code you are going to write creates a shopping basket of type String in which you add new items. Then your code will retrieve those items and print them to the console.

• You declare the array like this: String[ ] shoppingCart = new String[5];.

Let’s look at each part of this code.

○ The first part (before the assignment operator =) creates the array variable shoppingCart and denotes that it is a String type array: String[ ].

○ The second part creates the array and sets the size to 5: new String[5];.

Now you have an array called shoppingCart that is capable of storing five items.

• Next, you will want to store items in the array. You do this by calling the add() method and passing it the items you want to store in the array. You do this like so: shoppingCart[0] = “Hat”;.

Notice the square brackets after the array variable name. These brackets are required and allow you to specify where in the array you want to store the item. In the above code example, you are storing the word Hat at index position 0.

The shoppingCart array has five index positions numbered from 0 to 4—not 1 to 5. In Java, arrays are zero-based and start at 0. Be careful, as this will trip you up.

• You can continue adding items to the array, and your code might look like this:

class ShoppingBasket {
   public static void main(String[] args) {

     String[] shoppingCart = new String[5];
     shoppingCart[0] = “Hat”;
     shoppingCart[1] = “Keyboard”;
     shoppingCart[2] = “Soap”;
     shoppingCart[3] = “Chair”;
     shoppingCart[4] = “Carpet”;

   }
}

• Now that your array is full of items, you will want to retrieve those items and use them in some way. This is easily done by referencing the index number of the item you want to retrieve, like so: shoppingCart[3], and printing to the console, like so: System.out.println(shoppingCart[3]);. This code will print the word stored at index 3 and in the above case, print Chair to the console.

 

Step 3: Check and Run Your Program

Here is the code in full:

class ShoppingBasket {
   public static void main(String[] args) {

     String[] shoppingCart = new String[5];
     shoppingCart[0] = “Hat”;
     shoppingCart[1] = “Keyboard”;
     shoppingCart[2] = “Soap”;
     shoppingCart[3] = “Chair”;
     shoppingCart[4] = “Carpet”;

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

   }
}

Run your code, and you should see the word Chair printed to the console.

Congratulations, you have successfully completed your eighth Java program!

In Lesson 9, you will learn how to write code that selects a random element from an array. See you then.

 

Recommended book

Murach’s Java Programming by Joel Murach

 

Share with friends