la7.1_JavaArray

advertisement
Modul Praktikum Java Array
1.
Target




2.
Declarating and making array
Accessing elements inside of array
Determining amount of element in array
Declarating and making multidimension array
Background
At this session, we will be discussing about arrat at Java. First, we will
be definiting what array is, and then we will be definiting how to declarating and
using it at Java.
At before, we have been discusing how to declarates varies of variable
with using primitive data type. At declaring of variable, we often to sue a data
type along with the name of variable or unique identifier, where to using that
variable, we will be calling with identifier’s name.
As a sample, we have 3 variables with int data type that have a
different identifier for each variable.
int number1;
int number2;
int number3;
number1 = 1;
number2 = 2;
number3 = 3;
As u can see at the sample above, just for initialitation and using
variable especially in the same purpose, feel so confusing. In Java or in the other
programming language, they have ability to using one variable that can storing a
data list and then manipulating it to more effective. This stype of variable hat
called.
Versi 1.2
1|Page
Modul Praktikum Java Array
An array will be storing some of data items that having same of type
data at the memory block that close and then divided it to more slots. Imagine
array is a variable – a location memory that having one name as a identifier,
howeverit can storing more than a value.
3.
Experiment
Experiment1 Accessing element array:
public class ArraySample {
public static void main( String[] args ){
int[] ages = new int[100];
for( int i=0; i<100; i++ ){
System.out.print( ages[i] );
}
}
}
Experiment 2 Panjang Array:
Versi 1.2
2|Page
Modul Praktikum Java Array
public class ArraySample2 {
public static void main( String[] args ){
int[] ages = new int[100];
for( int i=0; i<ages.length; i++ ){
System.out.print( ages[i] );
}
}
}
Experiment 3 Array Multidimensi:
public class ArraySample3 {
public static void main( String[] args ){
//
Elemen 512 x 128 dari integer array
int[][] twoD = new int[512][128];
//
karakter array 8 x 16 x 24
char[][][] threeD = new char[8][16][24];
//
String array 4 baris x 2 kolom
String[][] dogs = {{ "terry", "brown" },
{ "Kristin", "white" },
{ "toby", "gray"},
{ "fido", "black"}
};
System.out.print( dogs[0][0] );
}
}
Experiment 4 Using Array args at main Method:
Versi 1.2
3|Page
Modul Praktikum Java Array
public class ArgsTest {
public static void main(String[] args) {
System.out.println("args[0] = " + args[0]);
System.out.println("args[1] = " + args[1]);
}
}
4.
Exercise
4.1 Days a week
Make an array string that will be initiatializing 7 days a weeks. As sample :Buatlah sebuah String array
yang akan menginisialisasi 7 hari dalam seminggu. Sebagai contoh,
String days[] = {“Monday”, “Tuesday”….};
Gunakan while-loop, kemudian print semua nilai dari array (Gunakan juga untuk do-while dan forloop)Using a while-loop.
4.2 Biggest number
Use BufferedReader and JOptoionPane, asking at user for 10 numbers. And then use Gunakanlah
BufferedReader dan JoptionPane, tanyakan kepada user untuk saving 10 numbers. Shows at user, , the
biggest input that will be given to user
Versi 1.2
4|Page
Modul Praktikum Java Array
4.3 Book address
Below is the multidimension arra y that pronounce the content of the book addressBerikut ini adalah
array multidimensi yang menyatakan isi dari sebuah buku alamat:
String entry = {{"Florence", "735-1234", "Manila"},
{"Joyce", "983-3333", "Quezon City"},
{"Becca", "456-3322", "Manila"}};
Print that book address at this format below
Name : Florence
Tel. #
: 735-1234
Address
: Manila
Name : Joyce
Tel. #
: 983-3333
Address
: Quezon City
Name : Becca
Tel. #
: 456-3322
Address
: Manila
Versi 1.2
5|Page
Download