Lab Class
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();
}
}
在这个班级中,我输入学生代码,姓氏和名字,并将它们放入学生班。
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);
}
}
}
在这里,我使用了一个哈希集和打印方法,我想打印我带进实验课的每个学生,但它只打印最后一个。如何解决此问题?在实验室中,我调用了方法addStudenteSet(s);
以下行有问题。
s = new Studente(matricola, cognome, nome);
//caricamento studenti nell'hashset
s.addStudenteSet(s);
您正在创建一个新对象"s"并将"s"添加到该对象内的哈希集中。 因此,每次迭代都会创建新对象,并且所有对象的哈希集都是相同的。
您可以将 HashSet studenti 设为静态,然后它对所有对象都相同。
公共静态集 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);
}
}
每个Studente
都有自己的Set<Studente>
,只包含一个Studente
(即当前学生本身,因为您使用s = new Studente(matricola, cognome, nome);
创建了一个新学生,然后使用s.addStudenteSet(s);
将其添加到自己的集合中)。
取而代之的是,您需要在void inserimento()
方法中Set<Studente>
:
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);
}
}