實驗班
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);
}
}
}
在這里,我使用了一個哈希集并進入了列印方法,我想列印我參加實驗室課程的每個學生,但它只列印最后一個。我該如何解決這個問題?在 Lab 類中,我呼叫了 addStudenteSet(s) 方法;
uj5u.com熱心網友回復:
以下行有問題。
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);
}
}
uj5u.com熱心網友回復:
每個Studente都有自己的Set<Studente>,只包含一個Studente(即當前的學生本身,因為您創建了一個新的學生,s = new Studente(matricola, cognome, nome);然后將其添加到自己的集合中s.addStudenteSet(s);)。
而不是Set<Studente>你的void inserimento()方法中需要一個:
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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/357439.html
上一篇:需要在另一個類中實作
下一篇:不能訪問同一個類中的私有成員
