Array
Baca Juga
1. Array Sederhana
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Tugas; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
int search; | |
System.out.print("Panjang array: "); | |
int n = scan.nextInt(); | |
int[] array = new int[n]; | |
for (int i = 0; i < n; i++) { | |
array[i] = scan.nextInt(); | |
} | |
System.out.print("Cari elemen: "); | |
search = scan.nextInt(); | |
for (int i = 0; i < search; i++) { | |
if (array[i] == search) { | |
System.out.println(array[i] + " ada diindex " + (i)); | |
System.out.println(); | |
break; | |
} | |
} | |
System.out.println("Menghapus index terahir"); | |
n--; | |
for (int i = 0; i < n; i++) { | |
System.out.print(array[i] + " "); | |
} | |
System.out.println(); | |
} | |
} |
2. Array sederhana dengan class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class LowArrayApp { | |
public static void main(String[] args) { | |
//variabel | |
LowArray array; | |
int elemen = 0; | |
int search, delete; | |
boolean found = false; | |
//Input fun | |
Scanner scan = new Scanner(System.in); | |
//Input array | |
System.out.print("Size array: "); | |
int n = scan.nextInt(); | |
array = new LowArray(n); | |
for (int i = 0; i < n; i++) { | |
array.set(i, scan.nextInt()); | |
elemen++; | |
} | |
//Get all array | |
for (int i = 0; i < elemen; i++) { | |
System.out.print(array.get(i) + " "); | |
} | |
System.out.println(); | |
//Search array | |
System.out.print("Search arrar: "); | |
search = scan.nextInt(); | |
for (int i = 0; i < elemen; i++) { | |
if(array.get(i) == search){ | |
found = true; | |
break; | |
} | |
} | |
if (found) { | |
System.out.println("Found " + search); | |
} else { | |
System.out.println("Can't find " + search); | |
} | |
//Delete array | |
System.out.print("Delete array: "); | |
delete = scan.nextInt(); | |
for(int i = 0; i < elemen; i++){ | |
if(array.get(i) == delete){ | |
continue; | |
} | |
System.out.print(array.get(i) + " "); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* LowArryay | |
*/ | |
public class LowArray { | |
private int arr[]; | |
public LowArray(int size) { | |
arr = new int[size]; | |
} | |
public void set(int i, int val){ | |
arr[i] = val; | |
} | |
public int get(int i){ | |
return arr[i]; | |
} | |
} |
3. Array lanjutan dengan class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class HighArrayApp { | |
public static void main(String[] args) { | |
HighArray array; | |
int search, delete; | |
Scanner scan = new Scanner(System.in); | |
System.out.print("Size array: "); | |
int n = scan.nextInt(); | |
array = new HighArray(n); | |
for (int i = 0; i < n; i++) { | |
array.insert(scan.nextInt()); | |
} | |
array.display(); | |
System.out.print("Search array: "); | |
search = scan.nextInt(); | |
if(array.find(search)){ | |
System.out.println("Found : " + search); | |
} else { | |
System.out.println("Can't find : " + search); | |
} | |
System.out.print("Delete array: "); | |
delete = scan.nextInt(); | |
array.delete(delete); | |
array.display(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HighArray { | |
private long[] array; | |
private int elemen; | |
public HighArray(int size){ | |
array = new long[size]; | |
elemen = 0; | |
} | |
public boolean find(long search){ | |
for (int i = 0; i < elemen; i++) { | |
if(array[i] == search){ | |
return true; | |
} | |
} | |
return false; | |
} | |
public void insert(long val){ | |
array[elemen] = val; | |
elemen++; | |
} | |
public boolean delete(int val){ | |
int j; | |
for(j = 0; j < elemen; j++){ | |
if(array[j] == val){ | |
break; | |
} | |
} | |
if(j == elemen){ | |
return false; | |
}else{ | |
for (int i = j; i < elemen - 1; i++) { | |
array[i] = array[i+1]; | |
} | |
elemen--; | |
return true; | |
} | |
} | |
public void display(){ | |
for (int i = 0; i < elemen; i++) { | |
System.out.print(array[i] + " "); | |
} | |
System.out.println(); | |
} | |
} |
4. Fungsi Sort Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.Scanner; | |
public class OrderArrayApp { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
System.out.print("Size array: "); | |
int n = scan.nextInt(); | |
int[] array = new int[n]; | |
for (int i = 0; i < n; i++) { | |
array[i] = scan.nextInt(); | |
} | |
Arrays.sort(array); | |
for (int i = 0; i < n; i++) { | |
System.out.print(array[i] + " "); | |
} | |
} | |
} |
Belum ada Komentar untuk "Array"
Posting Komentar