คุณสามารถแทนที่ toString() เมธอดของคลาส Object แต่ถ้าคุณกำลังสร้างอาร์เรย์อ็อบเจ็กต์ของคลาสใดคลาสหนึ่ง และต้องการพิมพ์เนื้อหาของอาร์เรย์นี้โดยแทนที่เมธอด toString() แทนเมธอด คุณจะไม่สามารถทำอย่างนั้นได้ ไม่มีวิธีแก้ไขสำหรับสิ่งนั้นใน Java ณ ตอนนี้
แต่คุณสามารถบรรลุสิ่งนี้ได้โดยใช้วิธีอื่นๆ -
การใช้เมธอด toString() ของคลาส Arrays
toString() เมธอดของคลาส Arrays ยอมรับอาร์เรย์สตริง (อันที่จริงอาร์เรย์ใดๆ ก็ตาม) และส่งคืนเป็นสตริง ส่งอาร์เรย์สตริงของคุณไปที่เมธอดนี้เป็นพารามิเตอร์ คุณสามารถส่งอาร์เรย์ของวัตถุไปยังวิธีนี้ได้
ตัวอย่าง
import java.util.Arrays;
class Student {
String name = "Krishna";
int age = 20;
Student(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "Name: "+this.name+" "+"Age: "+this.age;
}
}
public class Example {
public static void main(String args[]) {
Student std1 = new Student("Krishna", 20);
Student std2 = new Student("Radha", 25);
Student std3 = new Student("Trupthi", 30);
Student std4 = new Student("David", 35);
Student std5 = new Student("Moksha", 40);
Student students[] = {std1, std2, std3, std4, std5};
System.out.println(Arrays.toString(students));
}
} ผลลัพธ์
[Name: Krishna Age: 20, Name: Radha Age: 25, Name: Trupthi Age: 30, Name: David Age: 35, Name: Moksha Age: 40]
การใช้ asList() วิธีการของคลาส Arrays
เมธอดนี้ยอมรับอาร์เรย์เป็นอาร์กิวเมนต์และส่งกลับรายการออบเจ็กต์ ใช้วิธีนี้เพื่อแปลงอาร์เรย์เป็น Set
ตัวอย่าง
import java.util.Arrays;
class Student {
String name = "Krishna";
int age = 20;
Student(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "Name: "+this.name+" "+"Age: "+this.age;
}
}
public class Example {
public static void main(String args[]) {
Student std1 = new Student("Krishna", 20);
Student std2 = new Student("Radha", 25);
Student std3 = new Student("Trupthi", 30);
Student std4 = new Student("David", 35);
Student std5 = new Student("Moksha", 40);
Student students[] = {std1, std2, std3, std4, std5};
System.out.println(Arrays.asList(students));
}
} ผลลัพธ์
[Name: Krishna Age: 20, Name: Radha Age: 25, Name: Trupthi Age: 30, Name: David Age: 35, Name: Moksha Age: 40]
การใช้คลาส ArrayList
สิ่งนี้แตกต่างเล็กน้อยแต่เป็นวิธีแก้ปัญหา -
-
สร้างคลาสขยายคลาส ArrayList
-
เพิ่มวัตถุในชั้นเรียนนี้
-
ใช้ toString() วิธีการของคลาส ArrayList เพื่อพิมพ์เนื้อหา
ตัวอย่าง
import java.util.ArrayList;
import java.util.Arrays;
class Student {
String name = "Krishna";
int age = 20;
Student(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "Name: "+this.name+" "+"Age: "+this.age;
}
}
public class Example extends ArrayList<Object> {
public static void main(String args[]) {
Example obj = new Example();
obj.add(new Student("Krishna", 20));
obj.add(new Student("Radha", 25));
obj.add(new Student("Trupthi", 30));
obj.add(new Student("David", 35));
obj.add(new Student("Moksha", 40));
System.out.println(obj.toString());
}
} ผลลัพธ์
[Name: Krishna Age: 20, Name: Radha Age: 25, Name: Trupthi Age: 30, Name: David Age: 35, Name: Moksha Age: 40]