10x Blogs Directory

Thursday, March 5, 2009

Introduction to Arrays

  • Suppose we have here three variables of type int with different identifiers for each variable.
int number1;
int number2;
int number3;

number1 = 1;
number2 = 2;
number3 = 3;

As you can see, it seems like a tedious task in order to just initialize and use the variables especially if they are used for the same purpose.
  • In Java and other programming languages, there is one capability wherein we can use one variable to store a list of data and manipulate them more efficiently. This type of variable is called an array.
  • An array stores multiple data items of the same data type, in a contiguous block of memory, divided into a number of slots.

Declaration ofan Array

  • To declare an array, write the data type, followed by a set of square brackets[], followed by the identifier name.
  • For example,
int []ages;
or
int ages[];


Instantiation of an Array

  • After declaring, we must create the array and specify its length with a constructor statement.
  • Definitions:
    -Instantiation
  • In Java, this means creation
    -Constructor
  • In order to instantiate an object, we need to use a constructor for this. A constructor is a method that is called to create a certain object.
  • We will cover more about instantiating objects and constructors later.
  • To instantiate (or create) an array, write the new keyword, followed by the square brackets containing the number of elements you want the array to have.
  • For example,

//declaration int ages[];

instantiate objectages = new int[100];

or, can also be written as,

//declare and instantiate object

int ages[] = new int[100];

  • You can also instantiate an array by directly initializing it with data.
  • For example,int arr[] = {1, 2, 3, 4, 5};This statement declares and instantiates an array of integers with five elements (initialized to the values 1, 2, 3, 4, and 5).


Accessing Array Element

  • To access an array element, or a part of the array, you use a number called an index or a subscript.
  • Index number or Subscript
    -assigned to each member of the array, to allow the program to access an individual member of the array.
    -begins with zero and progress sequentially by whole numbers to the end of the array.
    -NOTE: Elements inside your array are from 0 to (sizeOfArray-1).
  • For example, given the array we declared a while ago, we have

//assigns 10 to the first element in the array ages[0] = 10;

//prints the last element in the array System.out.print(ages[99]);

  • NOTE:
    -once an array is declared and constructed, the stored value of each member of the array will be initialized to zero for number data.
    -for reference data types such as Strings, they are NOT initialized to blanks or an empty string “”. Therefore, you must populate the String arrays explicitly.


Coding Guidelines

  • It is usually better to initialize or instantiate the array right away after you declare it. For example, the declaration,

int []arr = new int[100]; is preferred over,

  • It is usually better to initialize or instantiate the array right away after you declare it. For example, the declaration,

int []arr = new int[100]; is preferred over,

int []arr; arr = new int[100];

int []arr; arr = new int[100];


Array Length
  • In order to get the number of elements in an array, you can use the length field of an array.
  • The length field of an array returns the size of the array. It can be used by writing,
    arrayName.length


Coding Guidelines

  • When creating for loops to process the elements of an array, use the array object's length field in the condition statement of the for loop. This will allow the loop to adjust automatically for different-sized arrays.
  • Declare the sizes of arrays in a Java program using named constants to make them easy to change. For example, final int ARRAY_SIZE = 1000; //declare a constant . . . int[] ages = new int[ARRAY_SIZE];


Multi-Dimensional Array

  • Multidimensional arrays are implemented as arrays of arrays.
  • Multidimensional arrays are declared by appending the appropriate number of bracket pairs after the array name.
  • To access an element in a multidimensional array is just the same as accessing the elements in a one dimensional array.
  • For example, to access the first element in the first row of the array dogs, we write, System.out.print( dogs[0][0] );
  • This will print the String "terry" on the screen.



No comments:

Post a Comment