Cari Blog Ini

Kamis, 21 April 2011

Parallel array

Parallel array is to use multiple arrays with the same index. For example we want to create multiple columns in parallel - an array of x in the first column, array y in the second column, an array of colors in the third column, and so on. Data for the i-th row can be obtained from each of these arrays. There's nothing wrong with this way, but this way as opposed to object-oriented philosophy that collect data related in one object. If we follow the rules like this, we do not have to imagine amaka data connection and the other one because all data will be grouped in one place.

Suppose I write a class like

{DataString class
/ / Data from one of the messages
int x, y; / / Position Message
Color color; / / Color message
}

To save data in several messages, we can use an array of type DataString [], which is then declared as instance variable with the name of the data so

DataString [] data;

The contents of the data value null until we create a new array, for example with

data = new DataString [JUMLAH_PESAN];

Once the array is made, the value of each element of the array is null. We want to store data in objects of type DataString, but no object is created. What we've created is just the container only. Element in it in the form of objects which we never made. For that element in it can we make the for loop like:

for (int i = 0; i data [i] = new DataString ();

Now we can take data every message with the data [i]. X, data [i]. Y, and data [i]. Color.

Last associated with a switch statement. Suppose we have a value from 0 to 11 months, which represents the month within one year from January to December. We want to print it on screen, with the command

switch (month) {
case 0:
bulanString = "January";
break;
case 1:
bulanString = "February";
break;
case 2:
bulanString = "March";
break;
case 3:
bulanString = "April";
break;
.
.
.
case 11:
bulanString = "December";
break;
default:
bulanString = "One month";
}

We can replace the entire switch command by using the array, for example with namaBulan array that is declared as follows:

static String [] namaBulan = {"January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December"};

Then we can replace the entire switch on top with

bulanString = namaBulan [month];

Tidak ada komentar:

Posting Komentar