Array
Program Array Satu
Dimensi dalam Java
class Array1D
{
public static void main(String args[]){
int[] tweety = new int[5];
double[] mickey = new double[5];
String[] goku = new String[5];
for(int i=0; i<5;i++){
tweety[i] =i+3;
mickey[i] =i+2.5;
goku[i] ="String
"+(i*2);
}
for(int i=0;i<5;i++){
System.out.println("tweety
= "+tweety[i]+", mickey =
"+mickey[i]+", goku
= "+goku[i]+" --> index ke
[ "+i+" ]");
}
}
Program Array Dua
Dimensi bertipe String
class Array2D
{
public static void main(String args[]){
//pendeklarasian array matrix dengan
String
String[ ][ ] arrNama = {
{"Ano
"," Lichin ","Iim
"},
{"Zhay "," Dicky ","Wimbo "},
{"Tommy "," Pliqu ","Jati "} };
//menampilkan matrix array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arrNama[i][j]);
System.out.print(" ");
}
System.out.print("\n");
}
System.out.println("\n\n");
}
}
Program untuk
menginput dan menampilkan sebuah matriks dua dimensi
import java.io.*;
class Matriks {
public static void main(String args[]) {
int[][] M = new int[3][4];
int i,j;
//input matriks
BufferedReader isi;
System.out.println("Input Elemen
Matriks M[3][4]");
try {
isi = new BufferedReader(new
InputStreamReader(System.in));
for(i=0; i<3; i++) {
for(j=0; j<4; j++) {
System.out.print("M["+i+"]["+j+"]
= ");
M[i][j]
= new Integer(isi.readLine()).intValue();
}
System.out.println("\n");
}
} catch (Exception e ){
System.out.println("Input
berupa integer aja");
}
//tampilkan isi matriks
System.out.println("Isi Matriks
M");
for(i=0; i<3; i++) {
for(j=0; j<4; j++) {
System.out.print(M[i][j]+" ");
}
System.out.println("");
}
}
}
Program pengiriman
sebuah array ke dalam fungsi (method) dalam Java
class
KirimArray {
//fungsi untuk menerima array string
void kalimat(String nama[]){
for(int i=0; i<nama.length; i++){
System.out.println(nama[i]);
}
System.out.print("\n \n");
}
//fungsi untuk menerima array unt
void nilai(int[] nil){
for(int i=0; i<nil.length; i++){
System.out.println(nil[i]);
}
System.out.println("\n
\n");
}
//fungsi main
public static void main(String args[]) {
KirimArray coba = new KirimArray();
int[] data ={1,2,3,4,5,6,7};
String data1[] =
{"Java","Is","Funny"};
//penggunaan fungsi dan parameter
coba.kalimat(data1);
coba.nilai(data);
}
}
Program menyalin
isi suatu array ke dalam array lain
class ArrCopy
{
public static void main(String args[]) {
int[] angka = {2,4,5,1,12,5};
int[] nomor = new int[6];
int i;
//tampilkan array angka[]
System.out.println("Array
pertama");
for(i=0; i<angka.length; i++) {
System.out.print(angka[i]+"
");
}
System.out.println("\n\nArray
yang kedua ");
//copy array angka ke array nomor
System.arraycopy(angka,0,nomor,0,6);
//tampilkan array nomor[]
for(i=0; i<nomor.length; i++) {
System.out.print(nomor[i]+"
");
}
System.out.println("\n");
}
}
Program mengurutkan
isi suatu Array
import
java.util.*;
public
class Dblsrt {
public static void main(String[] args) {
Double[] da = {new Double(3.1), new
Double(99), new Double(-66)};
print(da); // Print unsorted array.
Arrays.sort(da); // Sort array
print(da); // Print sorted array.
}
private static void print(Object[] oa) {
for (int i=0; i<oa.length; i++) {
System.out.print(oa[i] + "
");
}
System.out.println("");
}
}
0 komentar:
Post a Comment