Rabu, 19 Disember 2012

CHAPTER 7 : ARRAY

  The definition of an array is a numbered sequence of items, which are all of the same type. The number of items in an array is called the length of the array. The position number of an item in an array is called the index of that item. The type of the individual items in an array is called the base type of the array.

     The base type of an array can be any Java type, that is, one of the primitive types, or a class name, or an interface name. If the base type of an array is int, it is referred to as an "array of ints." An array with base type String is referred to as an "array of Strings." However, an array is not, properly speaking, a list of integers or strings or other values. It is better thought of as a list of variables of type int, or a list of variables of type String, or of some other type. As always, there is some potential for confusion between the two uses of a variable: as a name for a memory location and as a name for the value stored in that memory location. Each position in an array acts as a variable. Each position can hold a value of a specified type (the base type of the array). The value can be changed at any time. Values are stored in an array. The array is the container, not the values.


 EXAMPLE :)


-You want to record the temperature for each day of the year .You could have

double tempDay1, tempDay2, ...

all the way to tempDay365

-But this is cumbersome.Arrays are made for situations like this



CREATING ARRAYS



1.To create an array

double [] data; // data is a variable of type double []
data = new double [ 10 ]; // data now holds 10 double values
2.Combined Declaration and Creation

double [] data = new double[10];

3.When array is created, all values are initialized depending on array type:
  • Numbers: 0
  • Boolean: false
  • Object References: null


 TYPES OF ARRAY

    In Java an array is simply a sequence of numbered items of the same type. You can create arrays of the primitive data types (e.g., characters, ints, floats, doubles) and instances of a class (e.g., an array of circle objects).
  1. One-Dimensional array
  2. Two-Dimensional array
  3. .Multi Dimensional array

One-Dimensional array



Declaration and layout of memory for a one-dimensional array of ints.

 The declaration for an array of five integers called iaA, and the corresponding layout of memory. Notice that the array element indices range from iaA [0] through iaA [4].

LOOPS AND ARRAYS

Loops can be used to process array in several ways:
  1. Initialize an array to a specific value
  2. Input data into an array
  3. Print an array
  4. Find the sum and average of an array
  5. Determine the largest element in the array

MANIPULATE DATA INTO ARRAYS

  1. Searching a value
  2. Calculation
  3. Reverse element



Arrays of Objects



  • To create an array that holds 5 BankAccount objects

    BankAccount[] account;

account = new BankAccount[5];



  • To Create a BankAccount object

account[0] = new BankAccount();

  • To fill the array with BankAccount objects

for (int i = 0; i < account.length; i++) {
   account[i] = new BankAccount();
}
    


Object Deletion - Approach 1



  • Delete BankAccount B by setting the reference in position 1 to null.
  • Before deleting B


  • After deleting B

int delIdx = 1;
account[delIdx] = null;



Object Deletion - Approach 2



  • Delete BankAccount B by setting the reference in position 1
    to the last BankAccount.
  • Before deleting B



  • After deleting B
    int delIdx = 1, last = 4;
    account[delIndex] = account[last];
    account[last] = null;
Two-Dimensional array

consists of rows and columns.


Although we think of a 2 dimensional array as consisting of rows and columns, it is actually linearly stored by row. This is an important piece of information that will come into play when passing arrays as parameters in functions.

     


INITIALIZING ARRAYS
Like other data types, it is possible to declare and initialize an array at the same time.

int[] primes = { 2, 3, 5, 7, 11 };

Multi Dimensional array





















Tiada ulasan:

Catat Ulasan

Related Posts Plugin for WordPress, Blogger...