Kelas Array
A. //Menampilkan Array
public class Array {
public class Array {
public static void main (String[] args) {
//variable
int nilai1 = 10;
int nilai2 = 6;
System.out.println("Nilai1 : " + nilai1);
System.out.println("Nilai2 : " + nilai2);
//array
//tipedata[] namaarray ; --> deklarasi
//contoh : int []data;
//namaarray = new tipedata[ukuran array]; --> instansiasi
//data = new int [10];
/*
int data[];
data = new int [10];
System.out.println("data[10] : " + data [0]);
data [0] = 7;
System.out.println("data [0] : " + data[0]);
for (int i=0;i<=9;i++) {
data[i] = 9-i;
System.out.println("data [" + i + "] : " + data[i]);
*/
int x;
int data2[] = new int[] {5,7,3,9,4};
for (int i=0; i<=4; i++) {
x=0;
if (data2[i]==9) {
x=i;
System.out.println("data2 [" + i + "] : " + data2[i]);}}}}
B. public class Array {
public static void main (String[] args) {
//Mencari angka 9 terletak di index keberapa.
//System.out.println("\nMencari angka 9 terletak di index keberapa");
int x;
int data2[] = new int[] {5,7,3,9,4};
/*for (int i=0; i<=4; i++) {
x=0;
if (data2[i]==9) {
x=i;
System.out.println("data2 [" + i + "] : " + data2[i]);
//Mencari angka terbesar dan terkecil pada array
System.out.println("\nMencari angka terbesar dan terkecil pada array");
int max = 0;
int min = 5;
for (int i = 0; i < data2.length; i++) {
if (data2[i] > max) {
max = data2[i];
} else if (data2[i] < min) {
min = data2[i];
} }
System.out.println("Nilai Terbesar : " + max);
System.out.println("Nilai Terkecil : " + min);
//Mengurutkan isi array dari angka terkecil ke angka terbesar
System.out.println("\nMengurutkan isi array dari angka terkecil ke angka terbesar");
for(int i=0; i<4; i++){
int data, j;
for(j=4; j>=(i+1); j--) {
if(data2[j] < data2[j-1]) {
data = data2[j];
data2[j] = data2[j-1];
data2[j-1]=data;
}}
System.out.println("data2 [" + j + "] : " + data2[j]);
//Mengurutkan isi array dari angka terbesar ke nagka terkecil
System.out.println("\nMengurutkan isi array dari angka terbesar ke angka terkecil");
for(int i=4; i>0; i--){
int data, j;
for(j=0; j<=(i-1); j++) {
if(data2[i] > data2[j]) {
data = data2[i];
data2[i] = data2[j];
data2[j]= data;
for(int k=0;k<=4;k++){
System.out.println("data2 [" + k + "] : " + data2[k]);
}}}
System.out.println(" ");
*/
//Menghitung jumlah angka 7 dalam array data3
int data3[] = new int[] {5,7,3,9,4,5,4,3,7,7,10,9,8};
for (int i=0;i<=12;i++){
if(data3[7]==i){
int total;
total = data3[7]=i;
System.out.println("Banyaknya Angka 7 dalam Array adalah : " +total);
}}}}
C. //Menampilkan outpt :
//[1, 3, 5, 7, 9]
//[1, 3, 5, 7, 9, 11]
//[5, 7, 9, 0, 0, 0, 0, 0]
public class ArrayReallocationDemo { public static void main(String[] args) {
int[] data1 = new int[] { 1, 3, 5, 7, 9 };
printArray(data1);
int[] data2 = Arrays.copyOf(data1, 6);
data2[5] = 11;
printArray(data2);
int[] data3 = Arrays.copyOfRange(data1, 2, 10);
printArray(data3);
}
// print array elements
private static void printArray(int[] data) {
StringBuilder stringBuilder = new StringBuilder("[");
for (int i = 0; i < data.length; i++) {
stringBuilder.append(data[i]);
if (i < data.length - 1)
stringBuilder.append(", ");
}
stringBuilder.append("]");
System.out.println(stringBuilder);
}}
0 komentar:
Posting Komentar