如何合并数组列表元素?

如何合并数组列表元素?

问题描述:

我有'一个字符串例如=“该网站拥有的Java开发者年鉴等。复制所有的例子,并且这些例子直接粘贴到你的应用程序”后,令牌如何合并数组列表元素?

,做一些我想要的字符串例子,我有数组列表等:

ArrayList <token > arl = " "this site holds ", "holds all the examples ", "the examples from The Java Developers", " Copy and paste ") 

“这个网站持有”,我知道位置开始和结束字符串测试:星= 1个端= 3 “包含所有的实施例中”,我知道位置STAT = 3端= 6,我知道position stat = 5 end = 10, “复制并粘贴”我知道position stat = 14 end = “The Java Developers的例子” 17,

我们可以看到,arl中的某些元素重叠:“本网站拥有”,“拥有所有示例”,“来自The Java Developers的示例”。

这里的问题是,我该如何合并overlaping元素recived的ArrayList像

ArrayList的结果=“”这个网站拥有的Java开发人员的所有实例”,‘’复制和粘贴‘’;

这里我的代码:但只合并拳头elecment如果检查元素overloaping

public ArrayList<TextChunks> finalTextChunks(ArrayList<TextChunks> textchunkswithkeyword) { 
     ArrayList<TextChunks > result = (ArrayList<TextChunks>) textchunkswithkeyword.clone(); 
      //System.out.print(result.size()); 
      int j; 
      for(int i=0;i< result.size() ;i++) { 
       int index = i; 
       if(i+1>=result.size()){ 
        break; 
       } 
       j=i+1; 
       if(result.get(i).checkOverlapingTwoTextchunks(result.get(j))== true) { 
        TextChunks temp = new TextChunks(); 
        temp = handleOverlaping(textchunkswithkeyword.get(i),textchunkswithkeyword.get(j),resultSearchEngine); 
        result.set(i, temp); 
        result.remove(j); 
        i = index; 
        continue; 
      } 
     } 
     return result; 
    } 
} 

感谢avadce

+0

我不知道我明白你在问什么。你能澄清你的问题吗?也许通过使用一个看起来不像问题一部分的示例字符串? – jasonmp85 2010-06-02 04:25:55

+0

Sory因为我的英文很弱,我一直在编辑我的问题,希望你能理解! – tiendv 2010-06-02 04:44:56

以下应做到这一点,或者至少说明合并这些块的想法。基本上我正在摧毁现有的块并重新创建新的块。听起来很可怕,但简化了很多。我只是将这些单词存储在List中并遍历该单词列表以构建新的(合并!)块。

private List<TextChunks> finalTextChunks(List<TextChunks> textchunkswithkeyword) { 

    private List<TextChunks> result = new ArrayList<TextChunk>(); 
    private List<String> wordList = new ArrayList<String>(); 

    // store all words in an arraylist, words are stored at their correct positions, 
    // ignored words from the original text are represented by null entries 
    for (TextChunks chunk : textchunkswithkeyword) { 
    int start = chunk.getStartTextchunks(); 
    List<Token> tokens = chunk.getTokens(); // TODO - implement getTokens() in TextChunks class 
    for (int i = 0; i < tokens.length; i++) { 
     wordList.set(start+i, tokens.get(i).toString()); // TODO - overwrite toString() in Token class 
    } 
    } 

    // recreate the chunks 
    int start = 0; 
    boolean isChunk = false; 
    StringBuilder chunkBuilder; 

    for (int i = 0; i < wordList.size(); i++) { 
    String word = wordList.get(i); 
    if (word == null) { 
     if (isChunk) { 
     // end of chunk detected 
     TextChunk chunk = new TextChunk(chunkBuilder.toString().split(" "), start, i); 
     result.add(chunk); 
     isChunk = false; 
     } else { 
     // do nothing 
     } 
    } else { 
     if (isChunk) { 
     // chunk gets longer by one word 
     chunkBuilder.append(" ").append(word); 
     } else { 
     // new chunk starts here 
     chunkBuilder = new StringBuilder(word); 
     start = i; 
     isChunk = true; 
     } 
    } 
    if (isChunk) { 
    // create and add the last chunk 
    TextChunks chunk = new TextChunk(chunkBuilder.toString(), start, wordList.size()-1); 
    result.add(chunk); 
    } 
    return result; 
} 

(警告 - 绝对没有测试过,我既没有一个IDE也不手头编译)

编辑

改变了代码 - 你说,那TextChunk类包含一个令牌(单词?)数组。这只是三个简单的修改。

EDIT 2

最后的编辑 - 我部分地适应我的代码到你的类。你需要做什么

  1. 实现getTokens()方法TextChunks仅仅返回arrt
  2. 实施TextChunks构造函数的String(用空格隔开的话),开始和结束。您的Token类已经提供了一种静态方法,用于将令牌字符串中的字符串转换为
  3. 覆盖类Token中的toString()方法,以便仅返回令牌String。
+0

感谢您的帮助,但我认为,您的方式不能帮助我。 我的应用程序有两种类: - 类令牌{string}里 令牌是一个字符串:在这个类有像一些方法: 除去空间,从字符串做arraytoken。 - Class textchunk {array token。 int start,int end} class textchunk扩展标记具有数组标记和两个值开始和结束。 所以里面的Textchunk是数组令牌(字符串) – tiendv 2010-06-02 06:45:04

+0

你可以看到我的代码理解的东西,我不能清楚地解释 ! – tiendv 2010-06-02 07:22:41

+0

@tiendv - 我看过了,你从我的回答中删除了你的正面投票。这有点令人沮丧,因为它花了很长时间了解你的问题和你的代码并提供了一个实用的解决方案。你期望什么? – 2010-06-02 08:38:24