0

Ok i don't know what do we call this conversion, so i am just gonna post the situation wrt S3 :

I have a list of relative urls, like this:
[
"a/"
"a/f2.txt",
"a/b/",
"a/b/file0.txt",
"a/b/file3.txt"
]
here if the end has an extension, its a file, or else its a folder. but if you notice, each folder is itself represented in a path. I need it in some kind of nested folder like structure, like:

{ name:'a' ,
children : [
{ name:'b' ,
children : ['file0.txt','file3.txt']
} ,
"f2.txt"
]
}

how can i make such parsing possible? would appreciate some java/kotlin based approach

Comments
  • 0
    Been too long since I coded in Java, so I can't give a code example. But im concept, here's what I would do:

    Instantiate a Key Value map(or whatever they're called). And make the key a string, and the value an Object. Then you can loop through the array, and if it's a folder, you just insert it into the map.

    It's an incomplete solution, and probably far from the best, but maybe it can lead you the right way
  • 0
    Last time I had to do something like this, I extended the LinkedList class and added it a name.

    class Folder extends LinkedList{
    public name;
    public Folder(String name){
    this.name=name;
    super();
    }
    }

    Something like that. You may be better off working with Hashmaps, so you can folder.get(subFolderName) (I don't remember the exact method) but the principle is the same.
    You have to make an abstract superclass so you can have Folders and Files in the same thing too.
    And the parsing is the easiest part.
  • 0
    @KasperNS thanks but won't this make a 1 dimensional structure? like
    {
    "foldername" -> file,
    "foldername2" -> file
    ...
    }
    what i want is a file explorer like ui. if folder2 is inside folder 1, then we should be seeing it in a second click, not directly alongside folder1
  • 1
    @c3r38r170
    humm, this is interesting. a linklist consists of a nodes, where each node has a data and a pointer/reference to another node. what i need is a node pointing to many nodes, in case of a folder. So i think some customization of generic tree could work. let me try and see if it works. thanks for the direction!
  • 1
    @KasperNS after trying a lot of approaches this along with composition worked heavenly. Thanks !
Add Comment