从不同的类中访问Arraylist内容?

时间:2013-12-28 19:38:13

标签: java arraylist

我有一个简单的程序,它接受用户名和密码并将它们打印到控制台。但是,我在课堂上有一个arraylist" UserInfo"存储用户信息。我想通过循环遍历不同类中的arraylist来打印用户信息。是否可以访问不同类别的arraylist?

例如,在课程"输出"我有一个for循环遍历" UserInfo"中的arraylist。类。如果它在" UserInfo"内部,则该循环有效。类。但是在访问另一个类中的arraylist时它不起作用。

   Errors:
   UserForm.java:53: error: cannot find symbol

这应该是计划的结果:

User 1:

Name: Jack
Password: 123

import java.util.ArrayList;

 public class UserForm{


    public static void main(String[] args) {

    User u1 = new Info1("Jack", 123);
    User u2 = new Info2("Mack", 125);

    UserInfo d1 = new UserInfo();


    output v1 = new output(d1);
    Print p = new Print(v1);
    d1.addUser(u1);
    d1.addUser(u2);

    p.addPrint(d1);

    //p.UserList();//Prints number of users
    v1.outputDetails();// Prints user information
    //p.UserList();

    }

    }



class Print{

    //This class is used for printing the number of users
output out;

public Print(output out){
     this.out = out;
}
    private ArrayList<UserInfo> printDetails = new ArrayList<UserInfo>();

    public void addPrint(UserInfo s){
        printDetails.add(s);
     }

    public ArrayList<UserInfo> getUserList(){
        return printDetails;
    }

    public void UserList(){
        int i=1;
        for(UserInfo s : printDetails){

            System.out.println("User: "+i);
            //s.outputDetails();

            i++;
        }
    }
}


class output {
UserInfo userInfo;

public output(UserInfo userInfo){
     this.userInfo = userInfo;
}

    public void outputDetails(){
        int i=1;

        for(User m : userInfo.getUser()){//error addUser

            System.out.println("User: "+i);
            System.out.println();
            System.out.println("Name: "+m.name);
            System.out.println("Password: "+m.password);
            System.out.println();
            i++;
        } System.out.println();
    }
}


class UserInfo{

     //This class is used to store the user information in an arraylist
     private ArrayList<User> addUser = new ArrayList<User>();

     public void addUser(User b){
        addUser.add(b);
     }

    public ArrayList<User> getUser(){
        return addUser;
    }



 }

class Info1 extends User {

    public Info1(String name, int password) {

        super(name, password);

    }

 }

 class Info2 extends User {

    public Info2(String name, int password) {

        super(name, password);

    }

 }

abstract class User{

    String name;
    int password;

    public User(String name, int password){
        this.name = name;
        this.password = password;
    }

    public String getName(){
        return name;
    }
    public void setName(){
        this.name = name;
    }
    public int getPassword(){
        return password;
    }
    public void setPassword(){
        this.password = password;
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在UserInfo课程中实例化Output,然后才能使用addUser中的UserInfo列表。然后只需调用getUser()方法返回列表。 Output没有addUser因此can not find symbol

class output{

    UnserInfo userInfo = new UserInfo();        <<<----------------

    public void outputDetails(){
        int i=1;

        for(User m : userInfo.getUser()){       <<<-----------------
            System.out.println("Name: "+m.name);

            System.out.println("\nPassword: "+m.password);

            i++;
        } 
        System.out.println();
    }
}

修改

  

“现在没有错误,但程序没有打印名称和密码!它只打印”用户1“”

它没有打印任何内容,因为theOutput类中的UserInfo对象没有添加任何内容。您需要一个采用UserInfo

的构造函数
class output {
    UserInfo userInfo;

    public output(UserInfo userInfo){
         this.userInfo = userInfo;
    }

    ...
}

您在main中将UserInfo传递给Output

User u = new Info("Jack", 123);
UserInfo d1 = new UserInfo();
d1.addUser(u);

output v = new output(d1);

Print p = new Print();
p.addPrint(d1);

p.UserList();//Prints number of users
v.outputDetails();