I need to search through a list of folders that could have more folders inside it, and add a new folder depending what folder is its parent.(the path is stored as a String for eg = "root/MyCom/home/") Then i fill in a field with a new folder name and add it to the final folder(eg "home/").
Below as you can see , I can navigate to the right location and add the new folder to a current folder, My trouble is that i cannot ensure that currentFolder element is placed back in the list it came from
how could i add a folder to a list of folders, that could be within a list of folders,that could be within a list of folders and endless more?
YFUser user = (YFUser)getSession().getAttribute(SESSION_USER);
Folder newFolder = new Folder();
newFolder.setFolderName(foldername);
// this is the path string (root/MyCom/home/) split in the different folder names
String folderNames[] = folderLocationString.split("/");
int folderNamesLength = folderNames.length;
Folder root = user.getRoot();
Folder currentFolder = root;
for(int i=0;i<folderNamesLength; i++){
// because root is folderNames[i]
String folderName = folderNames[i];
int currentFolderSize = currentFolder.getChildren.getSize();
for(int o=1; o<= currentFolderSize ; o++){
if(currentFolder.getChildren().get(o) instanceof Folder){
if(folderName.equals(currentFolder.getChildren().get(o).getFolderName())){
currentFolder = currentFolder.getChildren().get(o);
if (i == counter){
//now i am inside the correct folder and i add it to the list of folders within it
//the trouble is knowing how to re add this changed folder back to the list before it
currentFolder.getChildren.add(newFolder);
}
}
}
}
}
this is a simple version of what i need to do
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
List<String> strings = new ArrayList<String>();
strings.add("Donkey");
strings.add("hello");
strings.add("me");
strings.add("you");
strings.add("everyone");
strings.add("not u");
int counter= strings.size();
for (int i=0 ; i< counter ; i++){
System.out.println(strings.get(i));
if(strings.get(i).equals("Donkey")){
strings.remove(i);
strings.add(i, "not a Donkey");
}
}
for(String s : strings){
System.out.println(s);
}
}
}
this will print out Donkey, hello, me, you, everyone, not u
and then
not a Donkey, hello, me, you, everyone, not u,
as you can see here i am replacing the "Donkey" string with "not a Donkey"
so in terms of my project, i need to get the parent folder from the list, add the new Folder and then remove the old parent folder with the new updated one.
edit:
this is my folder class that holds a list of hierarchy objects, which can be either fileInformations or Folders,`
package com.example.client;
@Entity
@DiscriminatorValue("F")
public class Folder extends Hierarchy {
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinTable(name = "FOLDER_JOIN_FILELOCATION", joinColumns = {
@JoinColumn(name = "folder_id") }, inverseJoinColumns = {
@JoinColumn(name = "file_information_id") })
private List<Hierarchy> children = new ArrayList<Hierarchy>() ;
@Column(name = "folder_name")
private String folderName;
@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
@JoinTable(name="FOLDER_JOIN_FOLDER",
joinColumns = @JoinColumn(name="parent_folder_id"),
inverseJoinColumns = @JoinColumn(name="folder_ID")
)
private Hierarchy parent;
public Folder(){
}
public String getFolderName() {
return folderName;
}
public void setFolderName(String folderName) {
this.folderName = folderName;
}
public List<Hierarchy> getChildren() {
return children;
}
public void setChildren(List<Hierarchy> children) {
this.children = children;
}
@Override
public void addChild(Hierarchy h) {
children.add(h);
}
public Hierarchy getParent() {
return parent;
}
public void setFolder(Hierarchy folder) {
this.parent = folder;
}
@Override
public String toString() {
String val = this.folderName;
val += "/";
for(Hierarchy h : children ){
val += h.toString();
}
return val;
}
public Hierarchy getChild(int index){
return children.get(index);
}
}
now this example below, when i take out an element of the list, edit it , i think peter is saying that this will edit the list directly, but when i run this code it does not
List<String> strings = new ArrayList<String>();
strings.add("Donkey");
strings.add("hello");
strings.add("me");
strings.add("you");
strings.add("everyone");
strings.add("not u");
int counter= strings.size();
for (int i=0 ; i< counter ; i++){
System.out.println(strings.get(i));
if(strings.get(i).equals("Donkey")){
String test = strings.get(i);
test += " not now though";
}
}
for(String s : strings){
System.out.println(s);
}
`