每次限制字符串创建对象

时间:2015-08-04 10:35:35

标签: java

我正在研究一种方法,我需要完整的文件夹路径,如/ abc / xyz / mln /,我正在使用重复的方法调用,如下面的示例代码:

public String getFolderDetails(String userid, Long folderId) 
            {
        String folderName = "";
        String folderPath = "";

        try {
                folderName = documentFilesName.getString("name");
                parentFolderId= documentFilesName.getLong("parentFolderId");
                if(documentFilesName.has("parentFolderId"));
                {

                    if(parentFolderId.exist)
                    {
                        folderPath = folderPath+"/"+folderName;
                        getFolderDetails(userid, parentFolderId);
                    }

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return folderPath;
    }

但是每次将folderName和folderPath分配给“”时,由于重复出现,它不会保留/追加folderName,并且每次都将新值分配给folderPath。

我可以在这里使用更好的方法吗?与StringBuffer / Builder一样,它总是创建新对象。

3 个答案:

答案 0 :(得分:2)

ang.service('SessionStorageService', ['$window',function ($window) {
        var service = {
            store: store,
            retrieve: retrieve,
            clear: clear,
            clearAll: clearAll
        };

        return service;


        function store(key, value) {
            $window.sessionStorage.setItem(key, angular.toJson(value, false));
        }

        function retrieve(key) {
            return angular.fromJson($window.sessionStorage.getItem(key));
        }

        function clear(key) {
            $window.sessionStorage.removeItem(key);
        }

        function clearAll() {
            $window.sessionStorage.clear();
        }


    }]);

您需要将状态public String getFolderDetails(String userid, Long folderId) { // This will be the start of recursion so initializing foldername and folderpath with blank getFolderDetails(userid, folderId, new StringBuilder(""), new StringBuilder("")); } // recursive method takes two extra parameters for path and name private String getFolderDetails(String userid, Long folderId, StringBuilder foldername, StringBuilder folderpath) { /// Then each time you dont create a new builder just append to it } 传递给递归方法,以便它可以继续附加到它。

StringBuilder

使用路径时,不应将folderpath.append(File.separator).append(folderName); 用作硬编码,因为它取决于文件系统,而应使用'/',如上所示。

答案 1 :(得分:1)

您可以将folderNamefolderPath变量作为参数提供给您的方法,因此:public String getFolderDetails(String userid, Long folderId)变为:public String getFolderDetails(String userid, Long folderId, String folderName, String folderPath)

对于第一个方法调用,您将提供空字符串变量,然后,当您执行递归调用时,只需执行getFolderDetails(userid, parentFolderId, folderName, folderPath);

或者,您可以在方法本身中填充外部StringBuilder,因此这一行:folderPath = folderPath+"/"+folderName;看起来像stringBuilder.append("/").append(folderName);

答案 2 :(得分:1)

您始终可以使用堆栈(LinkedList)

来避免递归
LinkedList<File> stack = new LinkedList<>();
stack.add(root);

while (!stack.isEmpty()) {
    File current = stack.removeFirst();

    ...
    if (someCondition) {
        stack.addLast(someChildFile);
    }
}