Answer the question
In order to leave comments, you need to log in
How to add elements of collections to an element of the same collection?
I am making a program according to the linker pattern (composite), the essence is as follows, for example, the user enters root/name/name.txt into the console. The program takes a string, passes it to another class that checks if the first element is "root/" if yes (this is a mandatory requirement, more on that later), then the class returns an array of Strings that takes the next class, I called it Builder.
My linker consists of three classes, the abstract class Component and its two subclasses: File (it can only print its name), Folder (in fact, it is a composite, it has an ArrayList).
In the Builder, we create a new Folder ("root"), with which we carry out all operations, there is a method that takes our array of Strings in our case ["name", "name.txt"] and makes it a collection without the first element that in our case without e-that "root/".
And now the crux of the matter, there should be a method that will check if there is an element Folder, then the subsequent elements of the collection should already be added to our object of the Folder class, and this object should also be stored somewhere, maximum in the root collection root/ which is also a Folder object. File - our sheet, which cannot store anything, the number of objects that can be stored in the Folder is not limited (conditionally of course), below is my code. with which i get "java.lang.IndexOutOfBoundsException"
import java.util.ArrayList;
import java.util.List;
public class Builder {
private static final String isFile = "."; //для того, чтобы находить класс
private List<Component> componentArray = new ArrayList<>(); //для перевода в созданные классы
private File file = new File(""); //для сравнения
private Folder folder = new Folder(""); // для сравнения
private Folder root = new Folder("root"); //наш корень всех папок и файлов
private StringToArray text = new StringToArray(); // объект нашего переводчика в массив
private String[] textArray = text.getArray(); // наш массив Strings
public void gettingPackage(){
doComponent(textArray);
buildTree(componentArray);
root.print();
}
private void doComponent(String[] array){
for (int i = 1; i < array.length; i++) {
if(array[i].contains(isFile)) {
componentArray.add(new File(array[i]));
} else {
componentArray.add(new Folder(array[i]));
}
}
}
private void buildTree (List<Component> rootFolder){
for (int i = 0; i < rootFolder.size(); i++) {
if(rootFolder.get(i).getClass() == folder.getClass()) {
List<Component> bufferList = new ArrayList<>();
for (int j = rootFolder.indexOf(i); j < rootFolder.size(); j++) {
rootFolder.get(i).add(rootFolder.get(j));
System.out.println("Компонент " + rootFolder.get(j) + " добавлен в папку"
+ rootFolder.get(i));
}
root.add(rootFolder.get(i));
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question