通过子串排序数组

时间:2017-03-30 09:51:41

标签: java arrays sorting

我需要通过子串排序数组,并输入缺少的部分。

当前版本:
“K1 \ SK1”

“K1 \ SK2”

“K1 \ SK1 \ SSK1”

“K1 \ SK1 \ SSK2”

“K2”

“K2 \ SK1 \ SSK1”

“K2 \ SK1 \ SSK2”

预期版本:

“K1”

“K1 \ SK1”

“K1 \ SK1 \ SSK1”

“K1 \ SK1 \ SSK2”

“K1 \ SK2”

“K2”

“K2 \ SK1”

“K2 \ SK1 \ SSK1”

“K2 \ SK1 \ SSK2”

我已经尝试通过(" /")将字符串拆分为数组,然后分别对数组的每个部分进行排序。但最终它太不方便而且难以掌握!

也许有人知道如何解决这个问题的简单方法。

谢谢!

3 个答案:

答案 0 :(得分:0)

您可以创建一个已排序的Set,即TreeSet<String>,用于存储结果。

然后迭代输入字符串,并为每个字符串将其添加到结果Set。您还扫描所有\个字符,并且对于找到的每个\字符,您从字符串的开头抓取子字符串,但不包括\,并将其添加到结果Set

Set现在包含您之后的结果。如果需要,您可以将其提取为数组。

答案 1 :(得分:0)

这里是Python代码,而不是JAVA,但我认为很容易使这段代码适应我所写的排序集的使用尽可能不言自明:

def lstAllPositionsOfSeparatorCharInString(strSepChar, strToQuery): 
    """Return a list with all the positions of a character strSepChar in a string strToQuery."""
    lstPositions = []
    intPos = strToQuery.find(strSepChar)
    while intPos != -1: # strSepChar was found, proceed
        lstPositions.append(intPos)
        intPos = strToQuery.find(strSepChar, intPos+1)
    #:while
    return lstPositions
#:def

lstStrings=[r"K1\SK1", r"K1\SK2", r"K1\SK1\SSK1", r"K1\SK1\SSK2", r"K2", r"K2\SK1\SSK1", r"K2\SK1\SSK2"]
print(lstStrings)
# Append missing items if any: 
strSepChar = r"\ "[0] # strSepChar will contain a single backslash
for item in lstStrings: 
    for posNo in lstAllPositionsOfSeparatorCharInString(strSepChar, item):
        if item[0:posNo] not in lstStrings: 
            lstStrings.append(item[0:posNo])
#:for :for :if
print( lstStrings )
# sort the list (in place):
lstStrings.sort()
print( lstStrings )

代码首先给出原始列表的输出,而不是扩展列表,最后是排序列表(set):

"""
'K1\\SK1', 'K1\\SK2', 'K1\\SK1\\SSK1', 'K1\\SK1\\SSK2', 'K2', 'K2\\SK1\\SSK1', 'K2\\SK1\\SSK2']
['K1\\SK1', 'K1\\SK2', 'K1\\SK1\\SSK1', 'K1\\SK1\\SSK2', 'K2', 'K2\\SK1\\SSK1', 'K2\\SK1\\SSK2', 'K1', 'K2\\SK1']
['K1', 'K1\\SK1', 'K1\\SK1\\SSK1', 'K1\\SK1\\SSK2', 'K1\\SK2', 'K2', 'K2\\SK1', 'K2\\SK1\\SSK1', 'K2\\SK1\\SSK2']
"""

答案 2 :(得分:0)

您可以尝试以下代码,还需要一些导入。

     public class Sort {  
/**
 * Sort.
 * @param list original list
 */
public void sortByDirectOrder(List<String> list) {
    checkContainsAll(list);
    list.sort(new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            int result;
            int length1 = o1.length();
            int length2 = o2.length();
            int length = length1 < length2 ? length1 : length2;
            int subResult = o1.substring(0, length).compareTo(o2.substring(0, length));
            if (subResult == 0) {
                 result = length1 < length2 ? -1 : length2 < length1 ? 1 : 0;
            } else {
                 result = subResult;
            }
            return result;
        }
        });      
    }

/**
 * Method for check and complement list of codes.
 * @param list original list
 */
private void checkContainsAll(List<String> list) {
    List<String> additionalList = new ArrayList<>();
    String substring;
    for (String code : list) {
        if (code.contains("\\")) {
            for (int index = 0; index != code.length(); index++) {
                index = code.indexOf("\\", index);
                if (index == -1) {
                    break;
                }
                substring = code.substring(0, index);
                if (!list.contains(substring) && !additionalList.contains(substring)) {
                    additionalList.add(substring);
                }
            }
        }
    }
    if (additionalList.size() > 0) {
        list.addAll(additionalList);
    }
}
}