Menghapus elemen array
import javax.swing.JOptionPane;
public class HapusArray {
public static void main(String[] args) {
int[] data = new int[5];
for(int index=0; index < data.length; index++) {
data[index] = (int) (Math.random()*100)+20;
}
System.out.println("Data awal");
System.out.println("---------");
for(int index=0; index < data.length; index++) {
System.out.println("data" + "[" + index + "]" + data[index]);
}
int cari = Integer.parseInt(JOptionPane.showInputDialog(null, "Data yang dihapus:"));
boolean ketemu = false;
for(int index=0; index < data.length; index++) {
if(data[index] == cari){
ketemu = true;
break;
}
}
if (ketemu == true) {
int[] temporary = new int[data.length-1];
int counter = 0;
for(int i=0; i<temporary.length; i++) {
if(data[i] == cari) {
counter++;
temporary[i] = data[counter];
counter++;
}
else
{
temporary[i] = data[counter];
counter++;
}
}
data = new int[temporary.length];
System.out.println("");
System.out.println("setelah penghapusan data");
System.out.println("------------------------");
for(int i=0; i<data.length; i++) {
data[i] = temporary[i];
System.out.println("data" + "[" + i + "]" + data[i]);
}
} else {
JOptionPane.showMessageDialog(null, "tidak ada");
}
}
}
output program:
Data awal
---------
data[0]105
data[1]66
data[2]98
data[3]65
data[4]107
hapus elemen
setelah penghapusan data
------------------------
data[0]105
data[1]66
data[2]65
data[3]107
Menyisipkan elemen array
import javax.swing.JOptionPane;
public class SisipArray {
public static void main(String[] args) {
int[] data = new int[5];
//int[] temporary = new int[data.length+1];
for(int index=0; index < data.length; index++) {
data[index] = (int) (Math.random()*100)+20;
}
System.out.println("Data awal");
System.out.println("---------");
for(int index=0; index < data.length; index++) {
System.out.println("data" + "[" + index + "]" + " = " + data[index]);
}
int sisip = Integer.parseInt(JOptionPane.showInputDialog(null, "Data yang ditambahkan:"));
int lokasi = Integer.parseInt(JOptionPane.showInputDialog(null, "Pada index ke:"));
if (lokasi<0 || lokasi>data.length-1) {
JOptionPane.showMessageDialog(null, "anda salah meng-inputkan index");
} else {
int[] temporary = new int[data.length+1];
for(int index=0; index < lokasi; index++){
temporary[index] = data[index];
}
temporary[lokasi] = sisip;
for(int index=lokasi; index < data.length; index++){
temporary[index+1] = data[index];
}
data = new int[temporary.length];
for (int index=0; index<data.length; index++) {
data[index] = temporary[index];
}
}
System.out.println(" ");
System.out.println("Setelah penyisipan");
System.out.println("------------------");
for(int index=0; index<data.length; index++) {
System.out.println("data" + "[" + index + "]" + " = " + data[index]);
}
}
}
output program:
Data awal
---------
data[0] = 32
data[1] = 111
data[2] = 87
data[3] = 106
data[4] = 22
elemen yang akan ditambahkan
menententukan index untuk elemen baru
Setelah penyisipan
------------------
data[0] = 32
data[1] = 111
data[2] = 87
data[3] = 76
data[4] = 106
data[5] = 22
0 komentar:
Posting Komentar
Please Give Your Feedback Or Message.
Thank You!!?