Good day.

Studying the topic of parsing lines, one question arose. If we have the following String[] array:

A1 , A1\BB1 , A1\BB1\CCC1 , A2 , A2\BB1 , A2\BB1\CCC1

and we remove the 1st and 3rd elements, counting from scratch:

A1 , A1\BB1\CCC1 , A2\BB1 , A2\BB1\CCC1

Is it possible to restore the missing elements by parsing the lines? How would it be better to implement it?

Thank you.

  • An array according to some rule is created? - Senior Pomidor
  • As I understand it, this is something like a folder structure. - Andrew Bystrov
  • Yes, that's right, there is a certain folder structure with missing elements. Let me correct the question a little. - Dmitry08
  • My question is - by what principle do you delete 1 and 3 elements and by what principle should they be added back? - Andrew Bystrov
  • Yes of course. In essence, these are the missing items in the folder tree. That is, if there are elements A1 and A1\BB1\CCC1 , then in this case the element A1\BB1 will be missing. - Dmitry08

3 answers 3

 import java.util.Map; import java.util.HashMap; import java.util.ArrayList; public class Converter { public static void main(String[] args) { String[] test = {"A", "A\\B\\C\\D\\E", "A\\B\\M\\Y"}; for(String s : normalizePath(test)) System.out.print(s + ", "); System.out.println(); } /* Основной метод, возвращающий правильный массив. */ public static String[] normalizePath(String[] s) { Node root = makeTree(s); ArrayList<String> result = new ArrayList<String>(); for (Map.Entry<String, Node> e : root.getChildren().entrySet()) addToRes(result, e.getValue(), ""); return result.toArray(new String[result.size()]); } /* Вспомогательный метод для рекурсивного обхода дерева */ private static void addToRes(ArrayList<String> result, Node n, String parent) { String currentPath = parent.equals("") ? n.getVal() : parent + "\\" + n.getVal(); result.add(currentPath); for (Map.Entry<String, Node> e : n.getChildren().entrySet()) addToRes(result, e.getValue(), currentPath); } /* Создатель дерева */ private static Node makeTree(String[] s) { Node root = new Node(""); for (String path : s) { Node node = root; for (String name : path.split("\\\\")) { node = addVal(node, name); } } return root; } /* Вспомогательный метод создателя дерева */ private static Node addVal(Node node, String val) { if (node.getVal().equals(val)) return node; Map<String, Node> children = node.getChildren(); if (children.containsKey(val)) return children.get(val); Node newNode = new Node(val); children.put(val, newNode); return newNode; } /* Элемент дерева */ private static class Node { private Map<String, Node> children = new HashMap<String, Node>(); private String val; public Node(String val) {this.val = val;} public Map<String, Node> getChildren() {return children;} public String getVal() {return val;} } } 

Result

 A, A\B, A\B\C, A\B\C\D, A\B\C\D\E, A\B\M, A\B\M\Y, 
  • Thank you very much. The program works fine with any input data. - Dmitry08
  • @ Dmitry08 Please. Problem interested, I decided to draw in the code. Namudil with HashMap'om only, but it was already too lazy to redo it. :) - GreyGoblin

Can.
I will give the general idea of ​​the algorithm:

In the loop, go through the elements of the array (lines with slashes) and build a tree, creating a corresponding. branch for each element: break the string (the next element of the array) into slashes ("a \ b \ c" -> "a", "b", "c", eg str.split ("\\\\"), stand on the top of the tree and see if there is a branch for the first el-splitting ("a"), if not, create. Take the found or created branch, go to the next splitting el-th and repeat the procedure, and so on, until don't get to the end of the split. You can recursively.

Then bypass the tree and build for each element a line listing all the vertices from the root to the given element (again, you can recursively - it will be different and inconvenient).

  • Thank you very much, I will try to implement this option. - Dmitry08

try this

 String path=""; for(int i=0;i<yourArray.lenght;i++) path+=(","+yourArray[i]); 

Then restore the old array String newArray=path.split(",")

  • But here there is one thing: only 4 elements will be traversed in the for statement of the current array. In the original, they are 2 more. - Dmitry08
  • These actions can be performed before removing items - Developer
  • one
    With your permission, I will clarify the question a little. Initially, we have only 4 elements and it is necessary to restore the missing 2. - Dmitry08
  • Well then List <String> list = new ArrayList <> (); - Developer
  • list.addAll (yourArray); list.add (firstValue, 1); list.add (secondValue, 3) - Developer