简体   繁体   English

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

[英]How can i display all my data on array on object

i want to create a method that displays all the data on the array of objects i made. 我想创建一种在我制作的对象数组上显示所有数据的方法。 i manage to create a search specific through the help of people in here. 我设法通过这里的人们的帮助来创建特定的搜索。 can i re use his code?? 我可以重用他的代码吗? how? 怎么样? The method i want to create is at the last part. 我要创建的方法在最后一部分。 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());
        }
    }
}             

Because you're using the legacy Dictionary interface , this gets a bit annoying, but it's possible to do this. 因为您使用的是旧版Dictionary接口 ,所以这有点烦人,但是可以这样做。

What you want to do is create an Enumeration of your elements, then iterate over that. 您要做的是创建元素的Enumeration ,然后对其进行迭代。 The instance is provided by calling Dictionary#elements() , or more specifically, mouseList.elements() . 通过调用Dictionary#elements() ,或更具体地说,通过mouseList.elements()提供实例。

You then iterate over it as prescribed by Enumeration . 然后,按照Enumeration规定对其进行迭代

It would actually be a lot easier if Mouse also overrode toString() , as that's the best way to print those sorts of complex objects. 如果Mouse也覆盖toString() ,实际上会容易得多,因为这是打印这类复杂对象的最佳方法。

Lastly, you make a lot of unnecessary calls in that method - you don't care about an ID or anything like that. 最后,您在该方法中进行了许多不必要的调用-您不需要关心ID或类似的东西。 You'll also not need to check for null , since if it's not in the set of elements, it won't be in the Enumeration . 您也不需要检查null ,因为如果它不在元素集中,则不会在Enumeration

If you want to get a little more modern...then use a Map instead. 如果您想更现代一点...那么请使用Map代替。 Hashtable already uses the Map interface, so you won't need to change your concrete object (although I would strongly recommend it). Hashtable已经使用了Map界面,因此您不需要更改具体对象(尽管我会强烈推荐)。 Iterating over the elements of a Map becomes a call to Map#entrySet() with a little more work. 遍历Map的元素将成为对Map#entrySet()的调用,需要进行更多工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM