2012-10-21 20 views
9

Eventuali duplicati:
How to print object content in correct way?Stampa di tutti gli oggetti in lista di array

ho bisogno di essere in grado di stampare gli oggetti studenti (tutte le variabili) nella mia lista di array. È possibile? Quando provo a stampare, emette questo tipo di cose, ad esempio [email protected]. Penso che sia hexadecimal o qualcosa

Questo è il mio codice:

package student; 

public class Student { 

    private String studentName; 
    private String studentNo; 
    private String email; 
    private int year; 


    public Student() { 
     this.studentName = null; 
     this.studentNo = null; 
     this.email = null; 
     this.year = -1; 
    } 

    public Student(String nName, String nNum, String nEmail, int nYr) { 
     this.studentName = nName; 
     this.studentNo = nNum; 
     this.email = nEmail; 
     this.year = nYr; 
    } 

    public void setStudentName(String newStudentName) { 
     this.studentName = newStudentName; 
    } 

    public void setStudentNo(String newStudentNo) { 
     this.studentNo = newStudentNo; 
    } 

    public void setEmail(String newEmail) { 
     this.email = newEmail; 
    } 

    public void setYear(int newYear) { 
     this.year = newYear; 
    } 

    public String getStudentName() { 
     return studentName; 
    } 

    public String getStudentNo() { 
     return studentNo; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public int getYear() { 
     return year; 
    } 
} 

package student; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner; 

public class studentTest { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 



     List<Student> Students = new ArrayList(); 


     Student student1 = new Student(); 

     student1.setStudentName("Bob Marley"); 
     student1.setStudentNo("N0002"); 
     student1.setEmail("[email protected]"); 
     student1.setYear(2); 

     Students.add(student1); 

     Student student2 = new Student(); 

     student2.setStudentName("Bill Harvey"); 
     student2.setStudentNo("N0003"); 
     student2.setEmail("[email protected]"); 
     student2.setYear(2); 

     Students.add(student2); 

     Student student3 = new Student(); 

     student3.setStudentName("John Beans"); 
     student3.setStudentNo("N0004"); 
     student3.setEmail("[email protected]"); 
     student3.setYear(2); 

     Students.add(student3); 


     System.out.println("Add new students: "); 
     System.out.println("Enter number of students to add: "); 
     int countStudents = input.nextInt(); 

     for (int i = 0; i < countStudents; i++) { 
      Student newStudents = new Student(); 


      System.out.println("Enter details for student: " + (i + 1)); 

      System.out.println("Enter name: "); 
      newStudents.setStudentName(input.next()); 

      System.out.println("Enter Number: "); 
      newStudents.setStudentNo(input.next());System.out.println("Search by student number: "); 



      System.out.println("Enter email: "); 
      newStudents.setEmail(input.next()); 

      System.out.println("Enter year: "); 
      newStudents.setYear(input.nextInt()); 
      Students.add(newStudents); 
     } 


    } 
} 
+1

L': tag [tag compiti a casa] è depricated. –

risposta

11

Override toString() metodo nella Student classe come di seguito:

@Override 
    public String toString() { 
     return ("StudentName:"+this.getStudentName()+ 
        " Student No: "+ this.getStudentNo() + 
        " Email: "+ this.getEmail() + 
        " Year : " + this.getYear()); 
    } 
4

È necessario definire public String toString() metodo nella classe Student. Per esempio:

public String toString() { 
    return "Student: " + studentName + ", " + studentNo; 
} 
+1

Se si sta utilizzando eclipse, è possibile farlo automaticamente facendo clic su Origine -> genera toString() – amit

+0

Questo potrebbe sembrare sciocco (mi dispiace sto ancora imparando) ma se definisco tale metodo come accedere all'arrayist da esso. – joe

+0

utilizzando i bean netti – joe

13

Ogni volta che si stampa qualsiasi istanza della classe, l'attuazione defaulttoString di Object classe si chiama, che restituisce la rappresentazione che hai trovato. Contiene due parti: - Type e Hashcode

Così, in [email protected] che si ottiene come output ->

  • student.Student è il Type e
  • 82701e è il HashCode

Quindi, y ou necessario eseguire l'override un metodo toString nella classe Student per ottenere richiesto String representation: -

@Override 
public String toString() { 
    return "Student No: " + this.getStudentNo() + 
      ", Student Name: " + this.getStudentName(); 
} 

Così, quando dalla classe main, si stampa la ArrayList, si invocherà il metodo toString per ogni istanza, che si overrided piuttosto che quello in Object classe: -

List<Student> students = new ArrayList(); 

// You can directly print your ArrayList 
System.out.println(students); 

// Or, iterate through it to print each instance 
for(Student student: students) { 
    System.out.println(student); // Will invoke overrided `toString()` method 
} 

in entrambi i casi suddetti, il metodo toString overrided in Student classe sarà richiamata e appro la rappresentazione di ogni istanza verrà stampata.

Problemi correlati