类中的ArrayList

时间:2015-02-06 00:16:47

标签: java arraylist

我有一个自定义类的arraylist,对于每个我想保留自己的计算机arraylist的每个人。这是我的课程代码:

class Services {
    public String name;
    public String path;
    public ArrayList<String> computers = new ArrayList<>();

    public Services(String name, String path, String computer) {
        this.name = name;
        this.path = path;
        this.computers.add(computer);
    }

    public String getName() {
        return name;
    }

    public String getPath() {
        return path;
    }

    public void addComputer(String computerName) {
        this.computers.add(computerName);
    }

}

在我的main方法中,我正在检查我的服务的arraylist以查找具有相同名称的对象,如果存在,那么我只想将其添加到该对象的arraylist中。

然而,这不起作用,似乎我最终只得到了一个不是每个服务对象特定的所有计算机的arraylist。

以下是我使用此类的主要方法的部分。

stream.iterator().forEachRemaining(x -> {
    try {
        final boolean[] nextLine = {false};
        lines(x.toAbsolutePath(), Charset.forName("UTF-16")).forEach(y -> {
            if (!nextLine[0]) {
                // Finding Separator
                if (y.contains("-----------")) {

                    //Letting the program know the next line is a service.
                    nextLine[0] = true;
                }
            } else {
                //Splitting the service name from the path.
                String[] service = y.split(" + ");
                final boolean[] exists = {false};
                String computerName = x.getFileName().toString().substring(0, x.getFileName().toString().length() - 4);

                allServices.iterator().forEachRemaining(z -> {

                    if (z.name.contains(service[0])) {
                        exists[0] = true;
                    }
                });
                if (!exists[0]) {

                    //Creating new service object if it does not exisit. 
                    //Params are Service name, service path, computer name
                    Services serviceToAdd = new Services(service[0], service[1], computerName);
                    //Then adding it to the list to be printed out later. 
                    allServices.add(serviceToAdd);
                } else {
                    //Service name already exists, just adding the computer name to its list of affected computers. 
                    allServices.iterator().forEachRemaining(z -> {
                        if (z.name.contains(service[0])) {

                            if (!z.computers.contains(computerName)) {
                                z.addComputer(computerName);
                            }
                        }
                    });
                }
            }
        }
                );
    } catch (Exception ex) {
    }
});

最终,我正在尝试编制服务列表。有了这个服务列表,我需要知道服务的名称,路径以及哪些计算机拥有它们。我从文件名中获取计算机名称。似乎每个服务的计算机列表是相同的列表,而不是每个服务的不同列表。我该如何解决?我需要列出一份清单吗?这似乎是多余的,表现不佳。

1 个答案:

答案 0 :(得分:0)

由于您没有向我们提供与您的帖子相关的最重要的main方法,因此很难告诉您缺少的内容。

所以,有了我所拥有的一些信息,这里有一个小算法可以根据您发布的代码遍历所有存储的计算机。这应该可以帮助你弄清楚你缺少什么,否则请发布main方法。

public static void main(String args[]) {
    String newComputer = "UberPC3000x";
    boolean alreadyExistSomeWhere = false;
    for (int i = 0; !alreadyExistSomeWhere && i < allMyServices.size(); i++) {
        for (int j = 0; !alreadyExistSomeWhere && j < allMyServices.get(i).computers.size(); j++) {
            alreadyExistSomeWhere = allMyServices.get(i).computers.get(j).equals(newComputer);
        }
    }

    if (!alreadyExistSomeWhere) //Then it means it was not found and you are free to add wherever you want.
    {

    }
}