如何在对象上的数组上显示我的所有数据

时间:2013-12-30 07:00:21

标签: java

我想创建一个方法,显示我所创建的对象数组的所有数据。 我设法通过这里的人的帮助创建一个特定的搜索。 我能用他的代码吗?怎么样?我想要创建的方法是最后一部分。 如何在对象上的数组上显示我的所有数据

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());
        }
    }
}             

1 个答案:

答案 0 :(得分:0)

因为你正在使用the legacy Dictionary interface,这有点烦人,但是可以这样做。

你想要做的是创建一个Enumeration元素,然后迭代它。该实例是通过调用Dictionary#elements()或更具体地mouseList.elements()来提供的。

然后迭代它as prescribed by Enumeration.

如果Mouse也覆盖toString(),那实际上要容易得多,因为这是打印这些复杂对象的最佳方式。

最后,你在那个方法中做了很多不必要的调用 - 你不关心ID或类似的东西。您也无需检查null,因为如果它不在元素集中,则不会在Enumeration中。

如果你想更现代一点......那么请改用MapHashtable已使用Map接口,因此您无需更改具体对象(尽管我强烈推荐)。迭代Map的元素会成为对Map#entrySet()的调用,但需要更多的工作。