从其他类访问ArrayList

时间:2017-10-16 20:53:49

标签: java arraylist

我很难找到如何从另一个类访问ArrayList。我阅读了数百个类似的主题,但无法从我想要做的事情中找出错误的错误。我在setLoginChoice下面应该从登录类读取ArrayList。我也尝试动态地创建ArrayList,因此可以使用来自用户的新条目进行更新。我想知道为什么当我读取ArrayList时给我空值。

修改

也许我很笨,但据我所知,我创建的ArrayList基于客户方法吗?(具有相同数量的参数)。所以,当我试图阅读"背信息" (来自数组中每个列表的一个元素)我实际上正在阅读"背景代码"从方法"客户"这给了我一个空值。什么是正确的方法:)?

  public class Customer {

   private static String customerId;
   private static String firstName, lastName;
   private static String address, dateOfBirth, telephoneNumber, cprNumber;
   private static String userName, password, logInCredentials;
   private static  int postCode;
   String auserName;
   String apassword;
   String apostCode;
   String adateOfBirth ;
   String afirstName ;
   String alastName ;
   String aaddress ;



        public Customer (String afirstName,String aaddress ,String alastName, int apostCode, String acprNumber, String atelephoneNumber, String adateOfBirth , String auserName, String apassword)
    {
        adateOfBirth = dateOfBirth;
        afirstName = firstName;
        alastName = lastName;
        apostCode = postCode;
        aaddress = address;
        acprNumber = cprNumber; 
        atelephoneNumber = telephoneNumber;
        apassword = password;
        auserName = userName;


    }

   public static boolean setLoginChoice (int choiceForLogIn) {  
    ArrayList <Customer> myUsers = (new Login()).login();

    Scanner input = new Scanner(System.in);
        System.out.println("Enter your username: ");
        inputUsername = input.nextLine();
        System.out.println("Enter your password: ");
        inputPassword = input.nextLine();
        System.out.println("before");

        for(int i = 0; i < myUsers.size(); i++){
            System.out.println("loopfor");
            System.out.println(myUsers.get(i).apostCode);


            /*if(myUsers.get(i).auserName.compareTo(getInputuserName()) == 0 && myUsers.get(i).apassword.compareTo(getInputPassword()) == 0)
            {
            System.out.println("Welcome");
            return true;

            }else {
                System.out.println("insideloop");
            }*/

        }
        System.out.println("after");
        return false;
    }




     public class Login{

       ArrayList<Customer> myUsers;

       public ArrayList<Customer> login() {

    // TODO Auto-generated method stub
    ArrayList<Customer> myUsers = new ArrayList<Customer>();
    Customer d = new Customer(null, null, null, 0, null, null, null, null, null);
    myUsers = new ArrayList<Customer>();

    myUsers.add(new Customer("User1", "bane", "User1nick", 7444, "111116-4421", "55555555", "11/05/1993", "User3", "Joe"));
    myUsers.add(new Customer("User2", "bane", "User2nick", 7444, "111116-4021", "55555555", "11/05/1993", "User3", "Stefan2"));
    myUsers.add(new Customer("User3", "bane", "User3nick", 7444, "111116-4021", "55555555", "11/05/1993", "User3", "Adi2"));
    myUsers.add(new Customer(d.getfirstName(), d.getAddress(), d.getlastName(), d.getPostCode(), d.getCprNumber(), d.getTelephoneNumber(), d.getdateOfBirth(), d.getUsername(), d.getPassword()));;

    return myUsers; 
 }


}

2 个答案:

答案 0 :(得分:0)

private static String customerId;
 private static String firstName, lastName;
 private static String address, dateOfBirth, telephoneNumber, cprNumber;

这些变量始终为空。

调试代码时,您可以看到将值覆盖为null。

    adateOfBirth = dateOfBirth;
    afirstName = firstName;
    alastName = lastName;
    apostCode = postCode;
    aaddress = address;
    acprNumber = cprNumber; 
    atelephoneNumber = telephoneNumber;
    apassword = password;
    auserName = userName;

因此所有元素在数组中都为null。因为执行此行时。

myUsers.add(new Customer("User1", "bane", "User1nick", 7444, "111116-4421", 
"55555555", "11/05/1993", "User3", "Joe"));

它调用Customer函数并且所有参数都是正确的,但是当它返回时,所有值都为null,如前所述。代码就像

 myUsers.add(null, null, etc...);

答案 1 :(得分:0)

您的构造函数存在问题。您应该使用构造函数中传递的数据初始化对象数据,如下所示:

public Customer (String afirstName,String aaddress ,String alastName, int apostCode, String acprNumber, String atelephoneNumber, String adateOfBirth , String auserName, String apassword){
    this.adateOfBirth = adateOfBirth ; // adateOfBirth instead of dateOfBirth
    this.afirstName = afirstName ; // ...
    this.alastName = alastName ;
    this.apostCode = apostCode ;
    this.aaddress = aaddress ;
    this.acprNumber = acprNumber ; 
    this.atelephoneNumber = atelephoneNumber ;
    this.apassword = apassword ;
    this.auserName = auserName ;
}

不确定您对静态字段的意图是什么。