如何在对象中实例化数组

时间:2013-12-22 17:20:31

标签: java arrays object null

我的项目中有两个课程。

第一个类名为Addressbook,第二个类名为Contact

public class AddressBook 
{

    public void assignAddressNumber(int[] addressNumber)  {
            System.out.println("Please insert address");
        }

    public static void main(String[] args) 
    {
        boolean exit = false;
        int input = 0;
        int c = 0;
        String fN = "";
        String lN = "";
        Scanner in = new Scanner(System.in);

        System.out.println("Menu");
        System.out.println("1) Add contact");
        System.out.println("2) Show contact list");
        System.out.println("3) Search contact");
        System.out.println("4) Save addressbook");
        System.out.println("5) Load addressbook");
        System.out.println("6) Exit");

        while (exit==false)
        {
            input = in.nextInt();

            if(input==1)
            {
                System.out.println("How many contacts do you want to add?");
                int newContacts = in.nextInt();
                for (int i = 0; i<newContacts; i++)
                {
                    System.out.println("Insert first name of contact " + (i+1));
                    fN = in.next();
                    System.out.println("Insert last name of contact " + (i+1));
                    lN = in.next();                   
                    Contact newEntry = new Contact(fN, lN, i);
                    System.out.println("Insert address of contact " + (i+1));
                    int adrNum = in.nextInt();
                    String adrName1 = in.next();
                    String adrName2 = in.next();
                    Contact.setAddress(adrNum, adrName1, adrName2, i);
                    System.out.print("Contact " + (i+1) + " is: ");
                    System.out.print(Contact.firstName[i]); //firstName[0]
                    System.out.print(" " + Contact.lastName[i]); //lastName[0]
                }
            }

            if (input==2)
            {


            }

            if (input==5)
            {
                exit=true;
            }
        }
    }            
}



public class Contact 
{
    public static String[] firstName = new String[100];
    public static String[] lastName = new String[100]; 

    public static int[] addressnumber = new int[100];
    public static String[] addressname = new String[100];    

    public Contact(String fN, String lN, int index)
    {
        fN = firstName[index];
        lN = lastName[index];
    }

    public static void setAddress(int adrNum, String adrName1, String adrName2,
            int index)
    {
        adrNum = addressnumber[index];
        adrName1 = addressname[index];
        addressname[index] += " " + adrName2; //Format: [AdrName1] + [AdrName2]
    }

    public static void main(String[] args)
    {

    }
}

我的问题:

为什么System.out.print(Contact.firstName[i]);中的打印声明Addressbook无法打印firstName[i]而是打印null

如何重写它以使其打印数组Contact.firstName[i]的初始化值?

2 个答案:

答案 0 :(得分:3)

作业从右到左完成。变化

public Contact(String fN, String lN, int index)
{
    fN = firstName[index];
    lN = lastName[index];
}

public Contact(String fN, String lN, int index)
{
    firstName[index] = fN;
    lastName[index] = lN;
}

你拥有它的方式,你将firstName[index]的值赋给变量fN,基本上什么都没做。它是你要改变的firstName[index],而不是相反。

答案 1 :(得分:1)

这不是面向对象的代码。你不应该像你那样拥有静态字段。让Contact保持单个对象并实例化这些对象的列表:

List<Contact> contacts = new ArrayList<Contact>();
contacts.add(new Contact(fN, lN));