开发者

File Hierarchy system with a web application logic question: List searching

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 开发者_Python百科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);
    }

`


I am not sure if understand your problem correctly. You say

My trouble is that i cannot ensure that currentFolder element is placed back in the list it came from

However, my understanding is that currentFolder is already inside the list, and simply changing the state of currentFolder is not affecting this fact. In all list implementations I know, a call to get() just returns a reference to an element in the list, without removing that element. Unless you explicitly remove currentFolder from its containing list, it is going it stay there.

Anyway, in general, the solution to similar problems is to keep a reference to the parent of the current folder:

  Folder currentFolder = root;
  Folder parentFolder = null;

  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){
            Folder childFolder = currentFolder.getChildren().get(o);

            if(folderName.equals(childFolder.getFolderName())){
                parentFolder = currentFolder;
                currentFolder = childFolder;

                if  (i == counter){
                    currentFolder.getChildren.add(newFolder);
                    // reorganize parentFolder.getChildren() or whatever
                }

            }
          }
      }

  }

One side note: in your outer loop

for(int i=0;i<=folderNamesLength; i++){

you go one too far - the condition should be

for(int i=0;i<folderNamesLength; i++){

Same applies for the inner loop.

Moreover you are incrementing i both above and inside the loop:

String folderName = folderNames[i++];

should be

String folderName = folderNames[i];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜