Program Array Menggunakan Java
Array merupakan sekumpulan data dengan tipe data yang sama dan memiliki fungsi yang sejenis. Contohnya yaitu ketika kita ingin menyimpan 10 data nilai mahasiswa. Kita tidak perlu membuat 10 variabel berbeda hanya untuk menyimpan 10 data tersebut yang sebenarnya memiliki fungsi yang sejenis, yaitu menyimpan data nilai mahasiswa. Kita hanya perlu membuat 1 variabel array yang dapat menyimpan 10 data dengan tipe yang sama. dengan begitu, program dapat lebih mudah untuk didevelop dan dimaintenance.
Program 1 : Array Sederhana
Pada java, deklarasi array cukup mudah untuk dilakukan. Berikut ini merupakan contoh program java yang mengimplementasikan array.
/** | |
* Array App Program | |
* Program to implement an array type conventionally | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since March 25th 2021 | |
* */ | |
package assignment_02; | |
/** | |
* Class ArrayApp | |
* Description : | |
* This class is used as main console application. | |
* | |
* Methods : | |
* public static void main(String[] args) | |
* */ | |
public class ArrayApp { | |
/** | |
* Main Method | |
* Description : | |
* This method is used as main method. | |
* | |
* @param args arguments to the console app while compiled and launched. | |
* */ | |
public static void main(String[] args){ | |
// INITIALIZE ARRAY VALUE AND SET THE NUMBER OF ELEMENTS BY ARRAY LENGTH | |
long[] arr; | |
arr = new long[100]; | |
arr[0] = 77; | |
arr[1] = 99; | |
arr[2] = 44; | |
arr[3] = 55; | |
arr[4] = 22; | |
arr[5] = 88; | |
arr[6] = 11; | |
arr[7] = 0; | |
arr[8] = 66; | |
arr[9] = 33; | |
int nElems = 10; | |
// DECLARE ANOTHER VARIABLE | |
int j; | |
long searchKey; | |
// DISPLAY ALL ELEMENTS OF THE ARRAY | |
for (j = 0; j < nElems; j++){ | |
System.out.print(arr[j] + " "); | |
} | |
System.out.println(); | |
// SEARCH ELEMENT IN ARRAY | |
searchKey = 66; | |
for (j = 0; j < nElems; j++){ | |
if(arr[j] == searchKey){ | |
break; | |
} | |
} | |
// DELETE FOUNDED ELEMENT | |
for (int k = j; k < nElems; k++){ | |
arr[k] = arr[k+1]; | |
} | |
nElems--; | |
// DISPLAY CURRENT ARRAY ELEMENT | |
for (j = 0; j < nElems; j++){ | |
System.out.print(arr[j] + " "); | |
} | |
System.out.println(); | |
} | |
} |
Pada program di atas, kita membuat array bertipe data long menggunakan cara konvensional. Ketika program dijalankan, maka hasilnya akan jadi seperti berikut
Program 2 : Implementasi Array Sederhana Menggunakan Class
Selain menggunakan fitur array yang sudah ada, kita juga dapat membuat class yang merepresentasikan array yang berisi data-data yang dibutuhkan oleh array, seperti data dan jumlah datanya. Berikut ini merupakan contoh program java yang menggunakan class baru sebagai representasi dari array.
/** | |
* Low Array Program | |
* Program to implement basic array type using class | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since March 25th 2021 | |
* */ | |
package assignment_02; | |
/** | |
* Class LowArray | |
* Description : | |
* This class represents an array type. | |
* | |
* Methods : | |
* public void setElem(int index, long value) | |
* public long getElem(int index) | |
* */ | |
public class LowArray { | |
private final long[] array; // REPRESENTS MAIN DATA | |
/** | |
* Constructor | |
* Description : | |
* Constructor will create new array with a specific size. | |
* | |
* @param size size of the array. | |
* */ | |
public LowArray(int size){ | |
array = new long[size]; | |
} | |
/** | |
* Method setElem | |
* Description : | |
* This method will set the specific index | |
* of the array with the specific value. | |
* | |
* @param index index of the array to be set. | |
* @param value value to be inserted to the array. | |
* */ | |
public void setElem(int index, long value){ | |
array[index] = value; | |
} | |
/** | |
* Method getElem | |
* Description : | |
* This method will return specific element | |
* of the array from the given index. | |
* | |
* @param index index of the array to be gotten. | |
* @return element of the array with the given index. | |
* */ | |
public long getElem(int index){ | |
return array[index]; | |
} | |
} | |
/** | |
* Class LowArrayApp | |
* Description : | |
* This class is used as main console application. | |
* | |
* Methods : | |
* public static void main(String[] args) | |
* */ | |
class LowArrayApp { | |
/** | |
* Main Method | |
* Description : | |
* This method is used as main method. | |
* | |
* @param args arguments to the console app while compiled and launched. | |
* */ | |
public static void main(String[] args){ | |
// INITIALIZE ARRAY OBJECT | |
LowArray arr; | |
arr = new LowArray(100); | |
int nElems; | |
// DECLARE ANOTHER VARIABLE | |
int j; | |
// ADD 10 ELEMENTS TO THE ARRAY | |
arr.setElem(0, 77); | |
arr.setElem(1, 99); | |
arr.setElem(2, 44); | |
arr.setElem(3, 55); | |
arr.setElem(4, 22); | |
arr.setElem(5, 88); | |
arr.setElem(6, 11); | |
arr.setElem(7, 66); | |
arr.setElem(8, 0); | |
arr.setElem(9, 33); | |
nElems = 10; | |
// DISPLAY ALL ELEMENTS OF THE ARRAY | |
for(j = 0; j < nElems; j++){ | |
System.out.print(arr.getElem(j) + " "); | |
} | |
System.out.println(); | |
// SEARCH ELEMENT FROM THE ARRAY | |
int searchKey = 26; | |
for (j = 0; j < nElems; j++){ | |
if (arr.getElem(j) == searchKey){ | |
break; | |
} | |
} | |
if (j == nElems){ | |
System.out.println("Can't find " + searchKey); | |
} else { | |
System.out.println("Found " + searchKey); | |
} | |
// SEARCH ELEMENT FROM THE ARRAY TO BE DELETED | |
for (j = 0; j < nElems; j++){ | |
if(arr.getElem(j) == 55){ | |
break; | |
} | |
} | |
// DELETE THAT ELEMENT FROM THE ARRAY | |
for (int k = j; k < nElems; k++){ | |
arr.setElem(k, arr.getElem(k+1)); | |
} | |
nElems--; | |
// DISPLAY CURRENT ARRAY ELEMENTS | |
for(j = 0; j < nElems; j++){ | |
System.out.print(arr.getElem(j) + " "); | |
} | |
System.out.println(); | |
} | |
} |
Pada program di atas, kita membuat class yang menjadi representasi dari array. Di dalam class tersebut terdapat method setElem yang digunakan untuk memberi nilai array pada index tertentu. Selain itu, juga terdapat method getElem yang digunakan untuk mendapatkan nilai array pada index tertentu. Ketika program dijalankan, maka hasilnya akan jadi seperti berikut.
Program 3 : Implementasi Array Lanjutan Menggunakan Class
/** | |
* High Array Program | |
* Program to implement advanced array type using class | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since March 25th 2021 | |
* */ | |
package assignment_02; | |
/** | |
* Class HighArray | |
* Description: | |
* This class represents an array type. | |
* | |
* Methods: | |
* public boolean find(long searchKey) | |
* public void insert(long value) | |
* public boolean delete(long value) | |
* public void display() | |
* */ | |
public class HighArray { | |
private final long[] array; // REPRESENTS MAIN DATA | |
private int nElems; // REPRESENTS NUMBER OF THE ELEMENTS OF THE MAIN DATA | |
/** | |
* Constructor | |
* Description : | |
* Constructor will create new array with a specific size. | |
* | |
* @param size size of the array. | |
* */ | |
public HighArray(int size){ | |
array = new long[size]; | |
nElems = 0; | |
} | |
/** | |
* Method find | |
* Description : | |
* This method is used to search element from the array. | |
* | |
* @param searchKey key to be looked for from the array. | |
* @return true if key found, else false. | |
* */ | |
public boolean find(long searchKey) { | |
int j; | |
for (j = 0; j < nElems; j++){ | |
if (array[j] == searchKey) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* Method insert | |
* Description : | |
* This method is used to insert element to a tail of the array. | |
* | |
* @param value value to be inserted to the array. | |
* */ | |
public void insert(long value){ | |
array[nElems] = value; | |
nElems++; | |
} | |
/** | |
* Method delete | |
* Description : | |
* This method is used to delete given value from the array. | |
* | |
* @param value value to be deleted from the array. | |
* @return true if the given value is found and deleted, else false. | |
* */ | |
public boolean delete(long value){ | |
int j; | |
for (j = 0; j < nElems; j++){ | |
if(value == array[j]){ | |
break; | |
} | |
} | |
if (j == nElems){ | |
return false; | |
} else { | |
for (int k = j; k < nElems; k++){ | |
array[k] = array[k+1]; | |
} | |
nElems--; | |
return true; | |
} | |
} | |
/** | |
* Method display | |
* Description : | |
* This method is used to display all elements of the array. | |
* */ | |
public void display(){ | |
for (int j = 0; j < nElems; j++){ | |
System.out.print(array[j] + " "); | |
} | |
System.out.println(); | |
} | |
} | |
/** | |
* Class HighArrayApp | |
* Description : | |
* This class is used as main console application. | |
* | |
* Methods: | |
* public static void main(String[] args) | |
* */ | |
class HighArrayApp { | |
/** | |
* Main Method | |
* Description : | |
* This method is used as main method. | |
* | |
* @param args arguments to the console app while compiled and launched. | |
* */ | |
public static void main(String[] args){ | |
// INITIALIZE THE ARRAY | |
int maxSize = 100; | |
HighArray arr; | |
arr = new HighArray(maxSize); | |
// ADD 10 ELEMENTS TO THE ARRAY | |
arr.insert(77); | |
arr.insert(99); | |
arr.insert(44); | |
arr.insert(55); | |
arr.insert(22); | |
arr.insert(88); | |
arr.insert(11); | |
arr.insert(0); | |
arr.insert(66); | |
arr.insert(33); | |
// DISPLAY ALL ELEMENTS OF THE ARRAY | |
arr.display(); | |
// SEARCH ELEMENT FROM THE ARRAY | |
long searchKey = 35; | |
if(arr.find(searchKey)){ | |
System.out.println("Found : " + searchKey); | |
} else { | |
System.out.println("Can't find : " + searchKey); | |
} | |
// DELETE SOME ELEMENT FROM THE ARRAY | |
arr.delete(0); | |
arr.delete(55); | |
arr.delete(99); | |
// DISPLAY CURRENT ELEMENT FROM THE ARRAY | |
arr.display(); | |
} | |
} |
Pada program di atas, ada beberapa method yang diimplementasikan. Pertama yaitu method find yang digunakan untuk mencari elemen tertentu di dalam array. Berikutnya yaitu method insert yang digunakan untuk menambahkan elemen baru pada array. Selanjutnya yaitu method delete yang digunakan untuk menghapus elemen tertertu pada array. Yang terakhir yaitu method display yang digunakan untuk menampilkan semua elemen pada array. Ketika program dijalankan, maka hasilnya akan jadi seperti berikut.
Program 4 : Implementasi Array Terurut Menggunakan Class
Kita juga dapat mengimplementasikan array terurut menggunakan class, dimana elemen-elemen didalamnya sudah secara otomatis terurut berdasarkan nilainya, bahkan ketika kita menambahkan elemen baru pada array tersebut. Berikut ini merupakan contoh implementasi array terurut menggunakan class.
/** | |
* Ordered Array Program | |
* Program to implement ordered array type using class | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since March 25th 2021 | |
* */ | |
package assignment_02; | |
/** | |
* Class OrderedArray | |
* Description: | |
* This class represents an ordered array type. | |
* | |
* Methods : | |
* public int size() | |
* public int find(long searchKey) | |
* public void insert(long value) | |
* public boolean delete(long value) | |
* public void display() | |
* */ | |
public class OrderedArray { | |
private final long[] array; // REPRESENTS MAIN DATA | |
private int nElems; // REPRESENTS NUMBER OF THE ELEMENTS OF THE MAIN DATA | |
/** | |
* Constructor | |
* Description : | |
* Constructor will create new array with a specific size. | |
* | |
* @param size size of the array. | |
* */ | |
public OrderedArray(int size){ | |
array = new long[size]; | |
nElems = 0; | |
} | |
/** | |
* Method size | |
* Description : | |
* This method will return size of the array. | |
* | |
* @return field nElems that represents the size of the array. | |
* */ | |
public int size() { | |
return nElems; | |
} | |
/** | |
* Method find | |
* Description : | |
* This method is used to search element from the array. | |
* This method is using binary search method. | |
* | |
* @param searchKey key to be looked for from the array | |
* @return index of the key if key found, else size of the array | |
* */ | |
public int find(long searchKey){ | |
int lowerBound = 0; | |
int upperBound = nElems - 1; | |
int curln; | |
do { | |
curln = (lowerBound + upperBound) / 2; | |
if (array[curln] == searchKey){ | |
return curln; | |
} else { | |
if (array[curln] < searchKey) { | |
lowerBound = curln + 1; | |
} else { | |
upperBound = curln - 1; | |
} | |
} | |
} while(lowerBound <= upperBound); | |
return nElems; | |
} | |
/** | |
* Method insert | |
* Description : | |
* This method is used to insert element to the array ordered by value | |
* by looking for index that have a greater value than a given value, | |
* then insert the given value before that value. | |
* | |
* @param value value to be inserted to the array. | |
* */ | |
public void insert(long value){ | |
int j; | |
for(j = 0; j < nElems; j++){ | |
if(array[j] > value){ | |
break; | |
} | |
} | |
for (int k = nElems; k > j; k--){ | |
array[k] = array[k-1]; | |
} | |
array[j] = value; | |
nElems++; | |
} | |
/** | |
* Method delete | |
* Description : | |
* This method is used to delete given value from the array. | |
* | |
* @param value value to be deleted from the array. | |
* @return true if the given value is found and deleted, else false. | |
* */ | |
public boolean delete(long value){ | |
int j = find(value); | |
if (j == nElems) { | |
return false; | |
} | |
for (int k = j; k < nElems; k++){ | |
array[k] = array[k+1]; | |
} | |
nElems--; | |
return true; | |
} | |
/** | |
* Method display | |
* Description : | |
* This method is used to display all elements of the array. | |
* */ | |
public void display(){ | |
for (int j = 0; j < nElems; j++){ | |
System.out.print(array[j] + " "); | |
} | |
System.out.println(); | |
} | |
} | |
/** | |
* Class OrderedArrayApp | |
* Description : | |
* This class is used as main console application. | |
* | |
* Methods: | |
* public static void main(String[] args) | |
* */ | |
class OrderedArrayApp { | |
/** | |
* Main Method | |
* Description : | |
* This method is used as main method. | |
* | |
* @param args arguments to the console app while compiled and launched. | |
* */ | |
public static void main(String[] args){ | |
// INITIALIZE THE ARRAY | |
int maxSize = 100; | |
OrderedArray arr; | |
arr = new OrderedArray(maxSize); | |
// ADD 10 ELEMENTS TO THE ARRAY | |
arr.insert(77); | |
arr.insert(99); | |
arr.insert(44); | |
arr.insert(55); | |
arr.insert(22); | |
arr.insert(88); | |
arr.insert(11); | |
arr.insert(0); | |
arr.insert(66); | |
arr.insert(33); | |
// SEARCH ELEMENT FROM THE ARRAY | |
long searchKey = 55; | |
if(arr.find(searchKey) != arr.size()){ | |
System.out.println("Found : " + searchKey); | |
} else { | |
System.out.println("Can't find : " + searchKey); | |
} | |
// DISPLAY ALL ELEMENTS OF THE ARRAY | |
arr.display(); | |
// DELETE SOME ELEMENT FROM THE ARRAY | |
arr.delete(0); | |
arr.delete(55); | |
arr.delete(99); | |
// DISPLAY CURRENT ELEMENT FROM THE ARRAY | |
arr.display(); | |
} | |
} |
Pada program di atas, terdapat beberapa method yang diimplementasikan. Pertama yaitu method size yang digunakan untuk mengetahui panjang array. Berikutnya yaitu method find yang digunakan untuk mencari elemen tertentu di dalam array. Karena semua elemen di dalam array sudah terurut, kita dapat mengimplementasikan binary search untuk method find. Selanjutnya yaitu method insert yang digunakan untuk menyisipkan elemen ke dalam array sesuai dengan urutan nilainya. Berikutnya yaitu method delete yang digunakan untuk menghapus elemen tertentu dari array. Yang terakhir yaitu method display yang digunakan menampilkan semua elemen di dalam array. Ketika program dijalankan, maka hasilnya akan jadi seperti berikut.
Program 5 : Implementasi Array Bertipe Data Objek
Selain menggunakan tipe data primitif sebagai tipe data elemennya, kita juga bisa menggunakan tipe data objek sebagai tipe data elemen array. Berikut ini merupakan contoh program java yang mengimplementasikan array dengan tipe data objek.
/** | |
* High Array Program | |
* Program to implement ordered array type using class | |
* | |
* @author Fajar Zuhri Hadiyanto | |
* @version 1.0 | |
* @since March 25th 2021 | |
* */ | |
package assignment_02; | |
/** | |
* Class Person | |
* Description : | |
* This class represents person object. | |
* | |
* Methods : | |
* public void displayPerson() | |
* public String getLastName() | |
* */ | |
class Person { | |
private final String lastName; // REPRESENTS LAST NAME OF THE PERSON | |
private final String firstName; // REPRESENTS FIRST NAME OF THE PERSON | |
private final int age; // REPRESENTS AGE OF THE PERSON | |
/** | |
* Constructor | |
* Description : | |
* Constructor will create new Person with the given lastname, firstname, and age. | |
* | |
* @param last lastname of the person. | |
* @param first firstname of the person. | |
* @param age age of the person. | |
* */ | |
public Person(String last, String first, int age){ | |
lastName = last; | |
firstName = first; | |
this.age = age; | |
} | |
/** | |
* Method displayPerson | |
* Description : | |
* This method will display all information of the person (firstname, lastname, and age). | |
* */ | |
public void displayPerson(){ | |
System.out.println("Last name : " + lastName); | |
System.out.println("First name : " + firstName); | |
System.out.println("Age : " + age); | |
} | |
/** | |
* Method getLastName | |
* Description : | |
* This method will return lastname of the person. | |
* | |
* @return lastname of the person. | |
* */ | |
public String getLastName(){ | |
return lastName; | |
} | |
} | |
/** | |
* Class ClassDataArray | |
* Description: | |
* This class represents array of person type. | |
* | |
* Methods: | |
* public Person find(String searchName) | |
* public void insert(String last, String first, int age) | |
* public boolean delete(String searchName) | |
* public void display() | |
* */ | |
public class ClassDataArray { | |
private final Person[] array; // REPRESENTS MAIN DATA | |
private int nElems; // REPRESENTS NUMBER OF THE ELEMENTS OF THE MAIN DATA | |
/** | |
* Constructor | |
* Description : | |
* Constructor will create new person array with a specific size. | |
* | |
* @param size size of the array. | |
* */ | |
public ClassDataArray(int size){ | |
array = new Person[size]; | |
nElems = 0; | |
} | |
/** | |
* Method find | |
* Description : | |
* This method is used to search person from the array by lastname. | |
* | |
* @param searchName lastname of the person to be looked for from the array. | |
* @return person object if found, else null. | |
* */ | |
public Person find(String searchName){ | |
int j; | |
for (j = 0; j < nElems; j++){ | |
if(array[j].getLastName().equalsIgnoreCase(searchName)){ | |
return array[j]; | |
} | |
} | |
return null; | |
} | |
/** | |
* Method insert | |
* Description : | |
* This method is used to insert element to the array. | |
* | |
* @param last lastname of the person to be added to the array. | |
* @param first firstname of the person to be added to the array. | |
* @param age age of the person to be added to the array. | |
* */ | |
public void insert(String last, String first, int age){ | |
array[nElems] = new Person(last, first, age); | |
nElems++; | |
} | |
/** | |
* Method delete | |
* Description : | |
* This method is used to delete element from the array by lastname. | |
* | |
* @param searchName lastname of the person to be deleted from the array. | |
* @return true if person found and deleted, else false. | |
* */ | |
public boolean delete(String searchName){ | |
int j; | |
for(j = 0; j < nElems; j++){ | |
if(array[j].getLastName().equalsIgnoreCase(searchName)){ | |
break; | |
} | |
} | |
if(j == nElems){ | |
return false; | |
} | |
for(int k = j; k < nElems; k++){ | |
array[k] = array[k+1]; | |
} | |
nElems--; | |
return false; | |
} | |
/** | |
* Method display | |
* Description : | |
* This method is used to display all person of the array. | |
* */ | |
public void display(){ | |
for(int j = 0; j < nElems; j++){ | |
array[j].displayPerson(); | |
} | |
} | |
} | |
/** | |
* Class ClassDataApp | |
* Description: | |
* This class is used as main console application. | |
* | |
* Methods: | |
* public static void main(String[] args) | |
* */ | |
class ClassDataApp { | |
/** | |
* Main Method | |
* Description: | |
* This method is used as main method. | |
* | |
* @param args arguments to the console app while compiled and launched. | |
* */ | |
public static void main(String[] args){ | |
// INITIALIZE THE ARRAY | |
int maxSize = 100; | |
ClassDataArray arr; | |
arr = new ClassDataArray(maxSize); | |
// ADD 10 ELEMENTS TO THE ARRAY | |
arr.insert("Evans", "Patty", 24); | |
arr.insert("Smith", "Loraine", 37); | |
arr.insert("Yee", "Tom", 43); | |
arr.insert("Adams", "Henry", 63); | |
arr.insert("Hashimoto", "Sato", 21); | |
arr.insert("Stimson", "Henry", 29); | |
arr.insert("Velasquez", "Jose", 72); | |
arr.insert("Lamarque", "Henry", 54); | |
arr.insert("Vang", "Minh", 22); | |
arr.insert("Creswell", "Lucinda", 18); | |
// DISPLAY ALL ELEMENTS OF THE ARRAY | |
arr.display(); | |
// SEARCH ELEMENT FROM THE ARRAY | |
String searchKey = "Stimson"; | |
Person found = arr.find(searchKey); | |
if(found != null){ | |
System.out.println("Found"); | |
found.displayPerson(); | |
} else { | |
System.out.println("Can't find " + searchKey); | |
} | |
// DELETE SOME ELEMENTS FROM THE ARRAY | |
System.out.println("Deleting Smith, Yee, and Creswell"); | |
arr.delete("Smith"); | |
arr.delete("Yee"); | |
arr.delete("Creswell"); | |
// DISPLAY CURRENT ELEMENTS OF THE ARRAY | |
arr.display(); | |
} | |
} |
Pada program di atas, kita menggunakan objek Person sebagai tipe data array. Ketika program dijalankan, maka hasilnya akan jadi seperti berikut.
Source code dari program-program di atas dapat diakses disini
Comments
Post a Comment