![](/img/trans.png)
[英]My array all contain "zeros" when I copy data from hashMap object to my array
[英]How can i display all my data on array on object
我想创建一种在我制作的对象数组上显示所有数据的方法。 我设法通过这里的人们的帮助来创建特定的搜索。 我可以重用他的代码吗? 怎么样? 我要创建的方法在最后一部分。 我如何在对象数组上显示所有数据
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Scanner;
public class TestMouse {
private static Dictionary<String, Mouse> mouseList = new Hashtable<String, Mouse>();
public static void main(String[] args) {
menu();
}
@SuppressWarnings("resource")
public static void menu() {
Scanner input = new Scanner(System.in);
int choice;
do {
System.out.println("CMPE 325 Student Record Holder System");
System.out.println("--------------------------------");
System.out.println("1.Add Student");
System.out.println("2.View Records");
System.out.println("3.Update Students");
System.out.println("4.Get Average");
System.out.println("5.Exit");
System.out.println();
System.out.println("--------------------------------");
System.out.print("Enter your choice: ");
choice = input.nextInt();
switch (choice) {
case 1:
record();
break;
case 2:
display();
// display();
break;
case 3:
menu();
// update();
// break();
case 4:
menu();
// average();
// break();
case 5:
break;
default:
continue;
}
} while (choice != 5);
System.out.println();
System.out.println("Good-Bye");
}
// end of menu(); method.
// ------------------------------------------------------------
@SuppressWarnings({ "resource", "unused" })
public static void record() {
Scanner Input = new Scanner(System.in);
int total;
System.out.println("How many students you are going to input? ");
total = Input.nextInt();
Mouse[] keyboard = new Mouse[total];
for (int index = 0; index < total; index++) {
String space;
keyboard[index] = new Mouse();
System.out.printf("Student[%d]", index + 1);
System.out.println();
System.out.println("");
space = Input.nextLine();
System.out.println("ID Number: ");
keyboard[index].setId(Input.nextLine());
System.out.println("First Name: ");
keyboard[index].setFirstName(Input.nextLine());
System.out.println("Middle Name: ");
keyboard[index].setMiddleName(Input.nextLine());
System.out.println("Last Name: ");
keyboard[index].setLastName(Input.nextLine());
System.out.println("Degree: ");
keyboard[index].setDegName(Input.nextLine());
System.out.println("Year Level: ");
keyboard[index].setYear(Input.nextInt());
//Save current object
mouseList.put(keyboard[index].getId(), keyboard[index]);
}
for (int index2 = 0; index2 < total; index2++) {
System.out.printf("Student[%d]", index2 + 1);
System.out.println();
System.out.println("ID Number:" + keyboard[index2].getId());
System.out.println("Name:" + keyboard[index2].getFirstName() + " "
+ keyboard[index2].getMiddleName() + " "
+ keyboard[index2].getLastName());
System.out.println("Degree:" + keyboard[index2].getDegName());
System.out.println("Year Level:" + keyboard[index2].getYear());
}
}
public static void specific() {
String id = "";
System.out.println("Enter an Id Number");
Scanner Input = new Scanner(System.in);
id = Input.nextLine();
Mouse mouse = mouseList.get(id);
if (mouse != null) {
System.out.println();
System.out.println("ID Number:" + mouse.getId());
System.out.println("Name:" + mouse.getFirstName() + " "
+ mouse.getMiddleName() + " "
+ mouse.getLastName());
System.out.println("Degree:" + mouse.getDegName());
System.out.println("Year Level:" + mouse.getYear());
}
}
@SuppressWarnings("resource")
public static void display(){
Scanner input = new Scanner(System.in);
int choice;
System.out.println("--------------------------------");
System.out.println("1.View List");
System.out.println("2.View Specific Record");
System.out.println("3.Exit");
System.out.println();
System.out.println("--------------------------------");
System.out.print("Enter your choice: ");
choice = input.nextInt();
switch (choice) {
case 1:
displayall();
break;
case 2:
specific();
break;
case 3:
menu();
break;
}
}
this part is the one im talking about. i hope u can help me
public static void displayall(){
String id = "";
System.out.println("Enter an Id Number");
Scanner Input = new Scanner(System.in);
id = Input.nextLine();
Mouse mouse = mouseList.get(id);
if (mouse != null) {
System.out.println();
System.out.println("ID Number:" + mouse.getId());
System.out.println("Name:" + mouse.getFirstName() + " "
+ mouse.getMiddleName() + " "
+ mouse.getLastName());
System.out.println("Degree:" + mouse.getDegName());
System.out.println("Year Level:" + mouse.getYear());
}
}
}
因为您使用的是旧版Dictionary
接口 ,所以这有点烦人,但是可以这样做。
您要做的是创建元素的Enumeration
,然后对其进行迭代。 通过调用Dictionary#elements()
,或更具体地说,通过mouseList.elements()
提供实例。
然后,按照Enumeration
规定对其进行迭代。
如果Mouse
也覆盖toString()
,实际上会容易得多,因为这是打印这类复杂对象的最佳方法。
最后,您在该方法中进行了许多不必要的调用-您不需要关心ID或类似的东西。 您也不需要检查null
,因为如果它不在元素集中,则不会在Enumeration
。
如果您想更现代一点...那么请使用Map
代替。 Hashtable
已经使用了Map
界面,因此您不需要更改具体对象(尽管我会强烈推荐)。 遍历Map
的元素将成为对Map#entrySet()
的调用,需要进行更多工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.