ในบทความนี้ เราจะเข้าใจวิธีการจัดเรียง arrayList ของออบเจกต์ที่กำหนดเองตามคุณสมบัติ คลาส TheArrayList ขยาย AbstractList และใช้อินเทอร์เฟซรายการ ArrayList รองรับอาร์เรย์ไดนามิกที่สามารถเติบโตได้ตามต้องการ
รายการอาร์เรย์ถูกสร้างขึ้นด้วยขนาดเริ่มต้น เมื่อเกินขนาดนี้ คอลเล็กชันจะถูกขยายโดยอัตโนมัติ เมื่อนำออบเจ็กต์ออก อาร์เรย์อาจหดตัว
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
สมมติว่าข้อมูลที่เราป้อนคือ −
The list is defined as Java Scala Python Mysql
ผลลัพธ์ที่ต้องการจะเป็น −
The list after sorting values: Java Mysql Python Scala
อัลกอริทึม
Step 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Use the ‘sort’ method to sort the list. Step 5 - Use the ‘compareTo’ method to compare properties of the list. Step 6 - Use the ‘add’ method to add new values to the list. Step 7 - In the main method, create an array list, and invoke the ‘sort’ method. Step 8 - Display the result Step 9 - Stop
ตัวอย่างที่ 1
ที่นี่ เราเชื่อมโยงการดำเนินการทั้งหมดเข้าด้วยกันภายใต้ฟังก์ชัน 'หลัก'
import java.util.*;
class CustomObject {
private String custom_property;
public CustomObject(String property){
this.custom_property = property;
}
public String get_custom_property(){
return this.custom_property;
}
}
public class Demo {
public static void print(ArrayList<CustomObject> input_list){
for (CustomObject object : input_list) {
System.out.println(object.get_custom_property());
}
}
public static void sort(ArrayList<CustomObject> input_list){
input_list.sort((object_1, object_2)
-> object_1.get_custom_property().compareTo(
object_2.get_custom_property()));
}
public static void add(ArrayList<CustomObject> input_list){
input_list.add(new CustomObject("Java"));
input_list.add(new CustomObject("Scala"));
input_list.add(new CustomObject("Python"));
input_list.add(new CustomObject("Mysql"));
}
public static void main(String[] args){
System.out.println("Required packages have been imported");
ArrayList<CustomObject> input_list = new ArrayList<>();
add(input_list);
System.out.println("The list is defined as ");
print(input_list);
sort(input_list);
System.out.println("\nThe list after sorting values: ");
print(input_list);
}
} ผลลัพธ์
Required packages have been imported The list is defined as Java Scala Python Mysql The list after sorting values: Java Mysql Python Scala
ตัวอย่างที่ 2
ในที่นี้ เราสรุปการดำเนินการเป็นฟังก์ชันที่แสดงการเขียนโปรแกรมเชิงวัตถุ
import java.util.*;
class CustomObject {
private String custom_property;
public CustomObject(String property){
this.custom_property = property;
}
public String get_custom_property(){
return this.custom_property;
}
}
public class Demo {
public static void main(String[] args){
System.out.println("Required packages have been imported");
ArrayList<CustomObject> input_list = new ArrayList<>();
input_list.add(new CustomObject("Java"));
input_list.add(new CustomObject("Scala"));
input_list.add(new CustomObject("Python"));
input_list.add(new CustomObject("Mysql"));
System.out.println("The number is defined as ");
for (CustomObject object : input_list) {
System.out.println(object.get_custom_property());
}
input_list.sort((object_1, object_2)
-> object_1.get_custom_property().compareTo(
object_2.get_custom_property()));
System.out.println("\nThe list after sorting values: ");
for (CustomObject object : input_list) {
System.out.println(object.get_custom_property());
}
}
} ผลลัพธ์
Required packages have been imported The number is defined as Java Scala Python Mysql The list after sorting values: Java Mysql Python Scala