Advertise here.

Saturday 25 August 2012

Java programming lesson #6 - Arrays


An array is group of variables that is given only one name. Each variable that makes up the array is given a number instead of a name which makes it easier to work with using loops among other things.

Declaring an array

Declaring an array is the same as declaring a normal variable except that you must put a set of square brackets after the variable type. Here is an example of how to declare an array of integers called a.


public class Array
{
   public static void main(String[] args)
   {
      int[] a;
   }
}


An array is more complex than a normal variable so we have to assign memory to the array when we declare it. When you assign memory to an array you also set its size. Here is an example of how to create an array that has 5 elements.


public class Array
{
   public static void main(String[] args)
   {
      int[] a = new int[5];
   }
}


Instead of assigning memory to the array you can assign values to it instead. This is called initializing the array because it is giving the array initial values.


public class Array
{
   public static void main(String[] args)
   {
      int[] a = {12, 23, 34, 45, 56};
   }
}

Using an array

You can access the values in an array using the number of the element you want to access between square brackets after the array's name. There is one important thing you must remember about arrays which is they always start at 0 and not 1. Here is an example of how to set the values for an array of 5 elements.

public class Array
{
   public static void main(String[] args)
   {
      int[] a = new int[5];
      a[0] = 12;
      a[1] = 23;
      a[2] = 34;
      a[3] = 45;
      a[4] = 56;
   }
}

A much more useful way of using an array is in a loop. Here is an example of how to use a loop to set all the values of an array to 0 which you will see is much easier than setting all the values to 0 seperately.

public class Array
{
   public static void main(String[] args)
   {
      int[] a = new int[5];
      for (int i = 0; i < 5; i++)
         a[i] = 0;
   }
}

Sorting an array

Sometimes you will want to sort the elements of an array so that they go from the lowest value to the highest value or the other way around. To do this we must use the bubble sort. A bubble sort uses an outer loop to go from the last element of the array to the first and an inner loop which goes from the first to the last. Each value is compared inside the inner loop against the value in front of it in the array and if it is greater than that value then it is swapped. Here is an example.

public class Array
{
   public static void main(String[] args)
   {
      int[] a = {3, 5, 1, 2, 4};
      int i, j, temp;
      for (i = 4; i >= 0; i--)
         for (j = 0; j < i; j++)
            if (a[j] > a[j + 1])
            {
               temp = a[j];
               a[j] = a[j + 1];
               a[j + 1] = temp;
            }
   }
}

2D arrays

So far we have been using 1-dimensional or 1D arrays. A 2D array can have values that go not only down but also across. Here are some pictures that will explain the difference between the 2 in a better way.

1D Array

01
12
23
34
45

2D Array

012
0123
1456
2789

All that you need to do to create and use a 2D array is use 2 square brackets instead of 1.

public class Array
{
   public static void main(String[] args)
   {
      int[][] a = new int[3][3];
      a[0][0] = 1;
   }
}

0 comments:

Post a Comment