[Exercise] Decomposing namespaces

/**
     * Recursively decompose namespaces
     * @param namespace
     * @return     the decomposed list collection
      */ 
    private  static List<String> recursion(String namespace) {
         int index = namespace.indexOf("." );
        List<String> arr= new ArrayList<>();
        if(index!=-1) {
            String currentName= namespace.substring(0, index);
            arr.add(currentName);
            String nextNamespace= namespace.substring(currentName.length()+1);
            arr.addAll(recursion(nextNamespace));
        }else {
            arr.add(namespace);
        }
        return arr;
    }
  
   recursion("aa.bb.cc");
 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325979150&siteId=291194637