Di bawah ini merupakan coding contoh program array 2 dimensi.
import java.util.Scanner;
public class Array2D
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int i;
int []data=new int [5];
for(i=0; i<5;i++)
{
System.out.print("Data ke "+i+" = ");
data[i]=in.nextInt();
}
System.out.print("Data Array : ");
for(i=0; i<5;i++)
{
System.out.print(" "+data[i]+",");
}
}
}
public class Array2D
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int i;
int []data=new int [5];
for(i=0; i<5;i++)
{
System.out.print("Data ke "+i+" = ");
data[i]=in.nextInt();
}
System.out.print("Data Array : ");
for(i=0; i<5;i++)
{
System.out.print(" "+data[i]+",");
}
}
}
Outputnya akan tampil seperti gambar dibawah ini.
Sekarang kita tambahkan hasilnya tetapi dengan posisi terbalik.
Data Array : 5, 6, 7, 8, 9
9, 8, 7, 6, 5
Bagaimanakah codingnya ? Langsung aja lihat coding programnya dibawah ini.
import java.util.Scanner;
public class Arrays2D
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int i;
int []data=new int [5];
for(i=0; i<5;i++)
{
System.out.print("Data ke "+i+" = ");
data[i]=in.nextInt();
}
System.out.print("Data Array :");
for(i=0; i<5; i++)
{
System.out.print(" "+data[i]+",");
}
System.out.print("");
System.out.print("\n \t");
for(i=4; i>=0; i--)
{
System.out.print(" "+data[i]+",");
}
}
}
public class Arrays2D
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int i;
int []data=new int [5];
for(i=0; i<5;i++)
{
System.out.print("Data ke "+i+" = ");
data[i]=in.nextInt();
}
System.out.print("Data Array :");
for(i=0; i<5; i++)
{
System.out.print(" "+data[i]+",");
}
System.out.print("");
System.out.print("\n \t");
for(i=4; i>=0; i--)
{
System.out.print(" "+data[i]+",");
}
}
}
Outputnya akan tampil seperti gambar dibawah ini.
1 komentar:
ini mah bukan array 2 dimensi.
Posting Komentar