如何将ArrayList传递给另一个类? Java的

时间:2018-01-02 19:34:28

标签: java arraylist javafx

我的代码遇到问题,我不太确定会出现什么问题。

(请忽略方法和班级名称。直到今天我都不知道规则。)

我需要将一个ArrayList从一个类发送到另一个类。

这是我要将数组传递给的类。

public class AdminUserController implements Initializable {

UserData callClass = new UserData();

public void CreateUser(){
    callClass.main();
    out.println("Button Clicked");
    ArrayTest(ArrayList <Users> myUsers);
}

public void ArrayTest(ArrayList <Users> myUsers){
    for (int i = 0; i < myUsers.toArray().length; i++) {
        if (myUsers.get(i) != null) {
            for (Users aMyUsers : myUsers) {
                if (aMyUsers != null) {
                    out.println("This should display 10 times");
                }
            }
        }
    }
}

此代码已隔离,并非完整程序。

CreateUser是一个JAVA FX按钮(我不确定是否可以通过这种方式传递参数)

我认为试图让ArrayTest方法激活的问题。

需要通过按下按钮激活它。

这是ArrayList所在的类。

        package BunnyCorp.Classes;

    import BunnyCorp.Controllers.AdminUserController;

    import java.util.ArrayList;

    public class UserData{

        public void main(){
        ArrayList <Users> myUsers = new ArrayList<>();

        Users myUsers0 = new Users(1000, "admin", "password", "N/A", " N/A", " N/A", "N/A", "N/A", "N/A", "N/A", "ADMIN");
        Users myUsers1 = new Users(1001, "lecturer", "capitalism", "Adam", "Smith", "127 High St", "Kirkcaldy", "KY5D8F", "03069990552", "16/06/1723", "STAFF");
        Users myUsers2 = new Users(1002, "president", "economics", "Ronald", "Reagan", "56 Bel-Air", "Los Caldy", "KY8BD2", "80865390552", "06/02/1911", "STAFF");
        Users myUsers3 = new Users(1003, "engineer", "memeword", "Tim", "Berners-Lee", "15 Westwood Place", "London", "KY9WWW", "07700900262", "08/06/1955", "STAFF");
        Users myUsers4 = new Users(1004, "polymath", "monalisa", "Leonardo", "da Vinci", "23 Via Mozza", "Florence", "FL9MLA", "07700900466", "15/04/1452", "MEMBER");
        Users myUsers5 = new Users(1005, "theoryman", "sexyphysics", "Richard", "Feynman", "Trump Tower", "New York", "NY5JEA", "03069990552", "11/05/1918", "MEMBER");
        Users myUsers6 = new Users(1006, "destroyerofworlds", "nuclearwar", "Julius Robert", "Oppenheimer", "20 Trinity Site", "New Mexico", "NKW194", "03069990828", "22/04/1904", "MEMBER");
        Users myUsers7 = new Users(1007, "crashtest", "dummy", "Samuel", "Alderson", "11 Marina Dey Rey", "California", "MDR238", "03069990011", "21/10/1914", "CASUAL");
        Users myUsers8 = new Users(1008, "Zucc", "dadada", "Mark", "Zuckerberg", "1456 Edgewood Drive", "California", "EWD238", "06505434800", "14/05/1984", "CASUAL");
        Users myUsers9 = new Users(1009, "bigmoney", "money", "Bill", "Gates", "1835 73rd Ave", "Washington", "WSA039", "2067093140", "28/10/1955", "CASUAL");

        myUsers.add(myUsers0);
        myUsers.add(myUsers1);
        myUsers.add(myUsers2);
        myUsers.add(myUsers3);
        myUsers.add(myUsers4);
        myUsers.add(myUsers5);
        myUsers.add(myUsers6);
        myUsers.add(myUsers7);
        myUsers.add(myUsers8);
        myUsers.add(myUsers9);

        AdminUserController UserCreator = new AdminUserController();
        UserCreator.ArrayTest(myUsers);
    }
}

我做错了吗?

我很感激帮助。我一直在网上寻找解决方案,但我一直在打墙。

1 个答案:

答案 0 :(得分:0)

不确定在构造函数中使用main方法执行了哪些操作,但是您的打印方法存在一些问题。

for (int i = 0; i < myUsers.toArray().length; i++) {
    if (myUsers.get(i) != null) {
        for (Users aMyUsers : myUsers) {
            if (aMyUsers != null) {
                out.println("This should display 10 times");
            }
        }
    }
}

1 /不需要myUser.toArray()。length,使用myUsers.size()
2 /在if语句中检查空用户后,只需将其打印出来,为什么再次使用循环遍历列表?它会打印两次arraylist

你的功能可能就像这样

for(Users aUser : myUsers){
  if(aUser!=null)
     System.out.println(aUser.toString());
}

for(int i = 0; i < myUsers.size(); i++){
   if(myUsers.get(i) != null)
      System.out.println(myUsers.get(i).toString());
}

如果你想知道当传递给函数时它们是否存在myUser数组,那么打印出它的大小或第一个元素就足够了。调试可能有助于解决这些问题。