Computer >> 컴퓨터 >  >> 프로그램 작성 >> Java

Java를 사용하여 배열에서 toString 메서드를 재정의할 수 있습니까?

<시간/>

toString() 을 재정의할 수 있습니다. Object 클래스의 메서드이지만 특정 클래스의 객체 배열을 생성하고 대신 toString() 메서드를 재정의하여 이 배열의 내용을 인쇄하려는 경우 Java에는 이에 대한 솔루션이 없습니다. 현재로서는.

그러나 다양한 다른 방법을 사용하여 이를 달성할 수 있습니다 -

Arrays 클래스의 toString() 메소드 사용

toString() Arrays 클래스의 메서드는 String 배열(사실상 모든 배열)을 허용하고 이를 String으로 반환합니다. String 배열을 이 메소드에 매개변수로 전달하십시오. 이 메서드에 개체 배열을 전달할 수 있습니다.

예시

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]

Arrays 클래스의 asList() 메소드 사용

이 메서드는 배열을 인수로 받아들이고 List 개체를 반환합니다. 이 방법을 사용하여 배열을 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]