0

例子:

String wholeString = 
"Typical models for star formation assume that every type of galaxy produces stars"

我想将拆分后的字符串及其以下 (+1) 字符串存储在树图中: with windowSize = 4(predefined): Typiypic-> 放入 TreeMap ypicpica-> 放入 TreeMap

因为windowSize = 2它看起来像这样: Ty, yp-> TreeMap

等等。

到目前为止我的代码:

    Map<String, String> generateMap = new TreeMap<String, String>();

    for (int i = 0; i < wholeString.length(); i++) {
        generateMap
                .put((wholeString.substring((i),
                        Math.min((i + windowSize), wholeString.length()))),
                        (wholeString.substring(
                                (i + 1),
                                (Math.min((i + windowSize),
                                        wholeString.length())))));

    }

如果我 sysprint 它,我会得到这个: { Augen=Augen, Außen=Außen, Innen=Innen, Jauch=Jauch,等等

4

3 回答 3

1

给你(出于调试目的,我通过 LinkedHashMap 更改了 TreeMap,如果您愿意,可以放回 TreeMap):

import java.util.LinkedHashMap;
import java.util.Map;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Map<String, String> generateMap = new LinkedHashMap<String, String>();
        String wholeString = "Typical models for star formation assume that every type of galaxy produces stars";
        int windowSize = 4;
        for (int i = 0; i < wholeString.length(); i++) {
            int start = i;
            int end = Math.min(i + windowSize, wholeString.length());
            int start1 = Math.min(i + 1, wholeString.length() - 1);
            int end1 = Math.min(i + 1 + windowSize, wholeString.length());
            generateMap.put(wholeString.substring(start, end), wholeString.substring(start1, end1));

        }
        System.err.println(generateMap);
    }

}
于 2012-04-25T20:23:11.163 回答
1

我的看法:

final String wholeString =
  "Typical models for star formation assume that every type of galaxy produces stars";
final int windowSize = 4;
final Map<String, String> generateMap = new LinkedHashMap<String, String>();

final int limit = wholeString.length() - windowSize;
for (int i = 0; i < limit;) generateMap.put(
      wholeString.substring(i, i + windowSize),
      wholeString.substring(++i, i + windowSize));

for (Map.Entry<String, String> e : generateMap.entrySet())
  System.out.println(e.getKey() + " -> " + e.getValue());
于 2012-04-25T20:36:49.733 回答
1

如果你喜欢递归,你可以试试这个:

import java.util.LinkedHashMap;
import java.util.Map;


public class Subs {

    /**
     * @param args
     */

    public static void func(Map<String,String> map, String input, int windowSize) {
        if (input.length() <= windowSize) {
            map.put(input, input);
            return;
        } else {
            map.put(input.substring(0, windowSize), input.substring(1, windowSize + 1));
            func(map, input.substring(1), windowSize);
        }
    }
    public static void main(String[] args) {
        String wholeString = "Typical models for star formation assume that every type of galaxy produces stars";
        Map<String,String> ourMap = new LinkedHashMap<String, String>();
        int windowSize = 4;
        func(ourMap, wholeString, windowSize);
        System.out.print(ourMap);
    }
}

请注意,我使用的是“LinkedHashMap”,因此输出将按照您将值放入其中的顺序排列。如果需要放入 TreeMap,只需将 LinkedHashMap 替换为 TreeMap 并添加 import 语句即可。但是结果将按自然顺序排序,而不是您输入值的顺序。

于 2012-04-25T20:46:37.040 回答