需要用对象数组替换ArrayList

时间:2015-06-16 16:50:34

标签: java arrays arraylist

我使用 ArrayList 创建了'数据库',但我需要将其替换为 数组 的对象那么如果没有销毁这个类,我怎么能做这个改变呢?

import java.util.*;

// ArrayList to store Users.
public class Appointment {
    public static void main(String[] args) {
        String command = "";
        String date = "";
        String appointment = "";
        String time = "";
        int id = 0;
        ArrayList<User> data = new ArrayList<User>();
        Scanner input = new Scanner(System.in);
        while (!command.equalsIgnoreCase("Exit")) {
            System.out.println("Please Enter a Command or Type Exit: ");
            command = input.nextLine();
            if (command.equalsIgnoreCase("Add")) {
                System.out.print("Data: ");
                date = input.nextLine();
                System.out.print("Time: ");
                time = input.nextLine();
                System.out.println("Day of appointment:");
                day = input.nextLine();
                System.out.print("Appointment: ");
                appointment = input.nextLine();

                User validUser = new User(date, time, appointment);
                data.add(validUser);
                System.out.println("Successful!");
                System.out.println(""); //Adds an appointment to database if it's valid.
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

要将ArrayList转换为该类型的数组,请使用.toArray()。在这种情况下,它将是

User [] users = data.toArray(new User [0]);

答案 1 :(得分:0)

实现使用数组的唯一方法是在使用新的“add”命令时动态扩展数组。

因此,您需要此方法来更新和扩展数组;

public static User[] add(User[] userArray, User user) {
    //null array check
    if( userArray == null ) {
        userArray = new User[1];
        userArray[0] = user;

        return userArray;
    }

    User[] newArray = new User[userArray.length+1];

    int i = 0;
    for( ; i < userArray.length; i++ )
        newArray[i] = userArray[i];

    newArray[i] = user; //already incremented with post-increment operator

    return newArray;
}

整个测试代码如下;

import java.util.*;

// ArrayList to store Users.
public class TestAppointment {
    public static void main(String[] args) {
        String command = "";
        String date = "";
        String appointment = "";
        String time = "";
        int id = 0;
        User[] data = null;
        Scanner input = new Scanner(System.in);
        while (!command.equalsIgnoreCase("Exit")) {
            System.out.println("Please Enter a Command or Type Exit: ");
            command = input.nextLine();
            if (command.equalsIgnoreCase("Add")) {
                System.out.print("Data: ");
                date = input.nextLine();
                System.out.print("Time: ");
                time = input.nextLine();
//              System.out.println("Day of appointment:");
//              date = input.nextLine();
                System.out.print("Appointment: ");
                appointment = input.nextLine();

                User validUser = new User(date, time, appointment);
                //data.add(validUser);  // we dont need this anymore
                data = add(data, validUser);
                System.out.println("Successful!");
                System.out.println(""); // Adds an appointment to database if
                                        // it's valid.
            }
        }

        System.out.println(data.length);
        for(int i = 0; i < data.length; i++) {
            System.out.printf("%d: %s:%s:%s\n",i,data[i].getDate(),data[i].getTime(),data[i].getAppointment());
        }
    }

    public static User[] add(User[] userArray, User user) {
        //null array check
        if( userArray == null ) {
            userArray = new User[1];
            userArray[0] = user;

            return userArray;
        }

        User[] newArray = new User[userArray.length+1];

        int i = 0;
        for( ; i < userArray.length; i++ )
            newArray[i] = userArray[i];

        newArray[i] = user; //already incremented with post-increment operator

        return newArray;
    }

    private static class User {

        private String date;
        private String time;
        private String appointment;

        public User(String date, String time, String appointment) {
            this.date = date;
            this.time = time;
            this.appointment = appointment;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

        public String getAppointment() {
            return appointment;
        }

        public void setAppointment(String appointment) {
            this.appointment = appointment;
        }
    }

}

此测试代码的输出如下;

Please Enter a Command or Type Exit: 
add
Data: testData1
Time: testTime1
Appointment: testApp1
Successful!

Please Enter a Command or Type Exit: 
add
Data: testData2
Time: testTime2
Appointment: testApp2
Successful!

Please Enter a Command or Type Exit: 
exit
2
0: testData1:testTime1:testApp1
1: testData2:testTime2:testApp2

希望这会对你有所帮助。