【问题标题】:Hashset iterator print哈希集迭代器打印
【发布时间】:2021-12-25 22:26:29
【问题描述】:

实验课

import java.math.BigInteger;
import java.util.Scanner;

public class Lab {

    public static void main(String[] args) {

        Studente s;
        inserimento();
    }

    public static void inserimento() {

        Studente s = null;
        do {
            try {
                //inserimento matricola
                System.out.println("\nmatricola:");
                Scanner mat = new Scanner(System.in);
                String matrstring = mat.nextLine();
                if (matrstring.equals("")) {
                    break;
                }
                int matricola = Integer.parseInt(matrstring);

                //inserimento cognome
                System.out.println("\ncognome:");
                Scanner cog = new Scanner(System.in);
                String cognome = cog.next();

                //inserimento nome
                System.out.println("\nnome:");
                Scanner nom = new Scanner(System.in);
                String nome = nom.next();

                //caricamento studente
                s = new Studente(matricola, cognome, nome);
                //caricamento studenti nell'hashset
                s.addStudenteSet(s);

            } catch (Exception e) {
                System.out.println("Dati inseriti sbagliati");
            }
        } while (true);

        System.out.println("fine inserimento");

        s.print();

    }

}

在这门课中,我输入学生代码、姓氏和姓名,然后将它们放入 Studente 课中。

import java.util.*;


public class Studente {

    private int matricola;
    private String cognome;
    private String nome;
    private Set<Studente> studenti = new HashSet<Studente>();

    public Studente(int matricola, String cognome, String nome)  {
        
        this.matricola=matricola;
        this.cognome=cognome;
        this.nome=nome;
    }
    
    public void addStudenteSet(Studente s){
        this.studenti.add(s);
    }
    @Override
    public boolean equals(Object o){
        Studente st = (Studente) o;
        if(this.matricola==st.matricola){
            return true;
        }else return false; 
    }
    @Override
    public int hashCode(){
        return Integer.hashCode(matricola);
    }
    
    
    public void print(){
        Iterator<Studente> i = this.studenti.iterator();
        while(i.hasNext()){
            Studente student = i.next();
            System.out.println("matricola: " + student.matricola + "\ncognome: " +student.cognome+ "\nnome: " +student.nome);

        }
        
    }
}

在这里,我使用了一个 hashset 和打印方法,我想打印我带进实验室课的每个学生,但它只打印最后一个。我该如何解决这个问题?在 Lab 类中,我调用了 addStudenteSet(s) 方法;

【问题讨论】:

  • 为什么是三个Scanner 对象?样本输入是什么?
  • 因为我要取学生姓名的代码
  • 一个Scanner 对象应该足以读取来自System.in 的所有输入。尝试移除其他两个并仅使用一个对象。
  • 从 Java 1.5 开始,建议使用for(Studente s : yourSet) 代替迭代器进行迭代
  • 我认为迭代器不是问题

标签: java class oop iterator hashset


【解决方案1】:

以下行有问题。

s = new Studente(matricola, cognome, nome);
//caricamento studenti nell'hashset
s.addStudenteSet(s);

您正在创建一个新对象“s”并将“s”添加到该对象内的 HashSet 中。 所以每次迭代都会产生新的对象,并且所有对象的 HashSet 都是相同的。

您可以将 HashSet studenti 设为静态,然后它将对所有对象都相同。

public static Set studenti = new HashSet();

或者您可以检查以下代码(请注意,我只考虑您提到的问题,未修复任何其他实现问题)。

public class Lab {

    private static  Set<Studente> studenti = new HashSet<Studente>();

    public static void main(String[] args) {

        Studente s;
        inserimento();
    }

    public static void inserimento() {

        Scanner mat = new Scanner(System.in);
        Studente s = null;
        boolean isContinue=true;
        do {
            try {
                //inserimento matricola
                System.out.println("\nmatricola:");
                String matrstring = mat.nextLine();
                if (matrstring.equals("")) {
                    break;
                }
                int matricola = Integer.parseInt(matrstring);

                //inserimento cognome
                System.out.println("\ncognome:");
                String cognome = mat.nextLine();

                //inserimento nome
                System.out.println("\nnome:");
                String nome = mat.nextLine();

                //caricamento studente
                s = new Studente(matricola, cognome, nome);
                //caricamento studenti nell'hashset
                studenti.add(s);
                
                System.out.println("\nenter 1 to continue:");
                isContinue= Integer.parseInt(mat.nextLine())==1 ? true :false;
            } catch (Exception e) {
                System.out.println("Dati inseriti sbagliati");
                isContinue=false;
            }
        } while (isContinue);

        System.out.println("fine inserimento");

        print();

    }



 public static void print(){
        Iterator<Studente> i = studenti.iterator();
        while(i.hasNext()){
            Studente student = i.next();
            System.out.println("matricola: " + student.getMatricola() + "\tcognome: " +student.getCognome()+ "\tnome: " +student.getNome()+"\n");

        }
        
    }

}



public class Studente {

    private int matricola;
    private String cognome;
    private String nome;
    
    public int getMatricola() {
        return matricola;
    }

    public String getCognome() {
        return cognome;
    }

    public String getNome() {
        return nome;
    }
    public Studente(int matricola, String cognome, String nome)  {
            
        this.matricola=matricola;
        this.cognome=cognome;
        this.nome=nome;
    }

    @Override
    public boolean equals(Object o){
        Studente st = (Studente) o;
        if(this.matricola==st.matricola){
            return true;
        }else return false; 
    }
    @Override
    public int hashCode(){
        return Integer.hashCode(matricola);
    }
}

【讨论】:

    【解决方案2】:

    每个Studente 都有自己的Set&lt;Studente&gt;,它只包含一个Studente(即当前学生本身,因为您使用s = new Studente(matricola, cognome, nome); 创建了一个新学生,然后使用s.addStudenteSet(s); 将其添加到自己的集合中)。

    您需要在 void inserimento() 方法中添加一个 Set&lt;Studente&gt;,而不是这个:

    public static void inserimento() {
    
        Scanner scanner = new Scanner(System.in);
        Set<Studente> studenti = new HashSet<Studente>();
        do {
            try {
                //inserimento matricola
                System.out.println("\nmatricola:");
                String matrstring = scanner.nextLine();
                if (matrstring.equals("")) {
                    break;
                }
                int matricola = Integer.parseInt(matrstring);
    
                //inserimento cognome
                System.out.println("\ncognome:");
                String cognome = scanner.next();
    
                //inserimento nome
                System.out.println("\nnome:");
                String nome = scanner.next();
    
                //caricamento studente
                s = new Studente(matricola, cognome, nome);
                //caricamento studenti nell'hashset
                studenti.add(s);
                scanner.nextLine(); // added to skip the pending newline after scanner.next();
    
            } catch (Exception e) {
                System.out.println("Dati inseriti sbagliati");
            }
        } while (true);
    
        System.out.println("fine inserimento");
    
        for (Studente s: studenti) {
            System.out.println("matricola: " + student.matricola + "\ncognome: " +student.cognome+ "\nnome: " +student.nome);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2014-04-01
      • 2020-11-20
      • 2016-06-10
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      相关资源
      最近更新 更多