Java array tutorial

Home - Tutorials - Basics

This article explains how to use arrays in Java. With the included code examples it is easy to understand arrays behaviour. 

Tutorial info:

Name:Java array tutorial
Total steps:1
Product:See complete product

Bookmark Java array tutorial

AddThis Social Bookmark Button

Step 1 - Java array usage

Java array tutorial

Arrays are objects in Java. The array is a container object that holds a fixed number of variables of a single type. An array can hold primitive types and objects as well, but all item must be the same type. 

Let’s say you want to store the name of the days in an array, that means the following items:



So we have to create an array which can hold 7 String values. We can do this in the following way: 

  1. String[] days = new String[7];

The [] makes from the simple String variable a String array. 

Now we have the array object that is prepared to hold 7 String object. Now it’s time to save the days names into the array. Before make some coding you need to know that the first element of an array has the index of 0. It means that in our case the indexes are from 0..6 and not from 1..7. 

  1. days[0] = "Monday";
  2. days[1] = "Tuesday";
  3. days[2] = "Wednesday";
  4. days[3] = "Thursday";
  5. days[4] = "Friday";
  6. days[5] = "Saturday";
  7. days[6] = "Sunday";
  9. for (int i=0; i<days.length; i++) {
  10. 	System.out.println(days[i]);
  11. }

It is also possible to initialise the array right during the creation in the following form:

  1. String days2[] = {"Monday","Tuesday","Wednesday"};
  3. for (int i=0; i<days2.length; i++) {
  4. 	System.out.println(days2[i]);
  5. }

Please note that size of the array was not defined. In such cases the size of the array will the same as the number of item in the initialisation block. In this example it is 3.

Of course you can also declare the array variable first and allocates the memory later and initialise element after as here:

  1. String[] workdays;
  2. //…
  3. workdays = new String[5];
  4. //…
  5. workdays[0] = "Monday";
  6. workdays[1] = "Tuesday";
  7. workdays[2] = "Wednesday";
  8. workdays[3] = "Thursday";
  9. workdays[4] = "Friday";

Creating an array of primitive types is exactly the same as before. Here is a simple example:

  1. int[] test = {1,2,3,4,5};
  3. for (int i=0; i<test.length; i++) {
  4. 	System.out.println(test[i]);
  5. }

Multidimensional arrays

Sometimes a simple array is not enough to store the data properly. Let’s say you want to store the following data hierarchy:



In this case each car brand has some car types as well. Every item can be handled as a String but you don’t want to put everything in a simple String array with 12 places to hold every car type. What you probably want to have an array with size of 4 and each element contains an other array with size of 3.

To do this we need to create a 2 dimensional array. The syntax is quite similar to the simple one dimensional array. Just use an other [] brackets like this:

  1. String[][] carTypes = new String[4][3];

And the format with initialisation looks like this:

  1. String[][] cars = {  {"A4","A6","A8"},
  2. 	             {"320","535","740"},
  3. 		 {"C220","E270","S320"},
  4. 		 {"IS220","GS300","LS600"}   };

Arrays are not so frequently used in Java as for example in PHP as Java has a lot of other collection types which can be better in the concrete situation. 

Tags: java array explanation, arrays in java, array tutorial, array how-to

Java array tutorial - Table of contents
Step 1 - Java array usage

Total time: 0.2463