-1
我有我的自定义类的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);
}
}
在我的主要方法,我检查我的服务的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) {
}
});
最终,我试图编译一个服务列表。有了这个服务列表,我需要知道服务的名称,路径以及哪些计算机拥有它们。我从文件的名称获取计算机名称。看来每个服务的计算机列表是相同的列表,而不是每个服务的不同列表。我该如何解决?我需要列出清单吗?这似乎是多余的,表现不佳。
请问您可以添加您谈论的方法(main?)。在哪里添加到服务的ArrayList中? – jnd 2015-02-06 00:18:32
你能解释一下你正在尝试做什么,以及出了什么问题?如果您可以将代码归结到尽可能最小的数量,但仍然可以证明错误,那么有助于 – 2015-02-06 01:44:01
感谢您的额外解释。我会看看 – 2015-02-06 02:13:04