Java - 使用copyOf添加到数组

时间:2016-03-29 00:01:30

标签: java arrays copy

我正在为类做一个任务,它要求我创建一个数组并按用户的意愿添加它。这是我到目前为止所做的:

public void add(Scanner stdIn)
{
    entries = new String[1];
    Contact add = new Contact(); // Instantiate new Contact instance

    String name;
    System.out.print("Enter the contact's name: ");
    name = stdIn.next();
    add.setName(name); // set name in Contact class

    String address;
    System.out.print("Enter the contact's address: ");
    address = stdIn.next();
    add.setAddress(address); // set address in Contact class

    String phone;
    System.out.print("Enter the contact's phone number: ");
    phone = stdIn.next();
    add.setPhone(phone); // set phone number in Contact class

    String email;
    System.out.print("Enter the contact's email address: ");
    email = stdIn.next();
    add.setEmail(email); // set email address in Contact class

    final int N = entries.length;

    entries = Arrays.copyOf(entries, N + 1);

    entries[0] = add.toString();

    System.out.print(Arrays.toString(entries));

} // end add

我对使用数组并不太熟悉,因此尝试复制旧数组并使用旧信息创建新数组,以及添加新信息都是在暗示我。 toString方法如下所示:

    @Override // Overrides method from java.lang.Object
    public String toString() // Displays the info for a contact in order
    {
        return getName() + "\t" + getAddress() + "\t" + getPhone() +
                "\t" + getEmail();
    }

如果您需要进一步澄清,请告诉我们!谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

正如我在你的评论中提到的,你需要在方法之外定义数组(可以使用ArrayList),否则它将超出范围,并且在调用add方法之间数据将丢失。

下面是示例代码,演示如何对add()进行多次调用,每次将数组扩展为1并打印结果:

    import java.util.Arrays;
    import java.util.Scanner;

    public class ArrayCopy {
        String[] entries = new String[0];

        public void add(Scanner stdIn)
        {
            entries = Arrays.copyOf(entries, entries.length + 1);

            Contact add = new Contact(); // Instantiate new Contact instance

            String name;
            System.out.print("Enter the contact's name: ");
            name = stdIn.next();
            add.setName(name); // set name in Contact class

            String address;
            System.out.print("Enter the contact's address: ");
            address = stdIn.next();
            add.setAddress(address); // set address in Contact class

            String phone;
            System.out.print("Enter the contact's phone number: ");
            phone = stdIn.next();
            add.setPhone(phone); // set phone number in Contact class

            String email;
            System.out.print("Enter the contact's email address: ");
            email = stdIn.next();
            add.setEmail(email); // set email address in Contact class

            entries[entries.length-1] = add.toString();

            System.out.println(Arrays.toString(entries));

        } // end add

        public static void main(String[] args) {
            ArrayCopy program = new ArrayCopy();
            Scanner scan = new Scanner(System.in);
            String op = "";

            System.out.println("Press A to add a user or E to exit.");
            while(!(op = scan.nextLine()).equalsIgnoreCase("E")){
                switch(op){
                    case "A":
                        program.add(scan);
                        break;
                    case "E":
                        System.out.println("Good Bye.");
                        System.exit(0);
                }
            }

        }
    }