如何使用java正确实现链表

时间:2017-11-16 18:52:37

标签: java arrays sorting linked-list

我正在使用链接列表(JAVA)进行项目,但我无法弄清楚如何插入此人。我怎么能让开关工作?或者我应该使用其他东西?我还考虑过在main方法之外创建一个类,然后调用它,但它不能正常工作。任何帮助都会非常感激

public void listOfPeople() { // Beginning of the method listOfPeople where shows the employees

        //  *** instance the person object ad loading your variables
        Personqueue p1 = new Personqueue(); //saving the employee in a variable???
        p1.setFname("John");
        p1.setLname("Smith");
        p1.setDOA(15);
        p1.setPassportN(306589);
        p1.setNumber(1);
        vectorObj.add(p1); // add the employee in a vector

        Personqueue p2 = new Personqueue(); //saving the employee in a variable???
        p1.setFname("Paul");
        p1.setLname("Clooney");
        p1.setDOA(5);
        p1.setPassportN(30614584);
        p1.setNumber(2);
        vectorObj.add(p2); // add the employee in a vector



    }

    public void ascendingOrder(Vector<Personqueue> vector) { // bubble sort method to order the vector

        int j;
        boolean flag = true; // set flag to true to begin first pass
        Personqueue temp; // holding the variable temporarily

        while (flag) {

            flag = false; // set flag to false awaiting a possible swap
            for (j = 0; j < vector.size() - 1; j++) {

                if (vector.get(j).getNumber() > vector.get(j + 1).getNumber()) {

                    temp = vector.get(j); // swap elements
                    vector.set(j, vector.get(j + 1));
                    vector.set(j + 1, temp);
                    flag = true; // shows a swap occurred

                }

            }

        }

    }

    public void newPerson(int positionPerson) { // beginning of newPerson method

        String Option = null; // declaration of local variables that are used only in this method and don't use too much space
        Personqueue p = new Personqueue(); //instead of setting it to null, here we are calling 
                                                  // a constructor which was declared in Personqueue class.

//      switch (positionPerson) {
//      case 1: // insert a person at the start of the queue
//      //  p = new QueueStart();
//          break; // executes in order to end the switch in case one of the options is valid
//      case 2: // insert a person at a chosen point in the queue
//      //  p = new ChoosePosition();
//      case 3: // insert a person at the end of the queue
//          //  p = new EndQueue();
//          break;
//      default:
//          System.out.println("Invalid Option!!!"); // in case the option is not one of the cases above, print this...
//          return; // return to the do/while loop in Principal method
//      }

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Name:"); // user input
        try {
            Option = br.readLine();
            String name = Option; 

            p.setFname(name);

        } catch (IOException e) {
            System.out.println(e);

        }

        System.out.println("Surname:"); // user input
        try {
            Option = br.readLine();
            String lname = Option; 

            p.setLname(lname);

        } catch (IOException e) {
            System.out.println(e);

        }

        System.out.println("Date Of Arrival: "); // user input
        try {
            Option = br.readLine();
            int doa = Integer.parseInt(Option); // use parseInt in order to convert Integer to String to be read by BufferedReader.

            p.setDOA(doa);

        } catch (IOException e) {
            System.out.println(e);

        }

        System.out.println("Passport Number:"); // user input
        try {
            Option = br.readLine();
            int pn = Integer.parseInt(Option);

            p.setPassportN(pn);

        } catch (IOException e) {
            System.out.println(e);

        }

        System.out.println("Number:"); // user input
        try {
            Option = br.readLine();
            int no = Integer.parseInt(Option);

            p.setNumber(no);

        } catch (IOException e) {
            System.out.println(e);

        }

        vectorObj.addElement(p); // save all the data in the vector

        System.out.print("Saving Person :" + p.getFname()); // print to the user the name's been saved

        try {
            for (int i = 0; i < 6; i++) { 
                System.out.print(".");
                Thread.sleep(300); // suspend the "." execution for a specified period. 

            }

        } catch (InterruptedException e) { //  exception p o method thread //  catch for thread above

            e.printStackTrace(); 
        }

        System.out.println();
        System.out.println("Person: " + p.getFname() + " is saved!!!");

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {

            e.printStackTrace();
        }

    } // end of the Method newPerson

1 个答案:

答案 0 :(得分:0)

检查listOfPeople(...)中的变量,p2未正确使用。

查看Example of LinkedList in Java