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
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