简体   繁体   English

如何检查一对已经存在?

[英]How to check if a pair already exists?

I have a string say "abab" and im splitting it in pairs.(ie ab,ab) If pair already exists then i dont want it to be generated.How do i do it Here's the code for what ive tried 我有一个说“ abab”的字符串,我将其成对分裂。(即ab,ab)如果已经存在一对,那么我就不想生成它。我该怎么做这是我尝试过的代码

      String r="abab";
      String  pair[] = new String[r.length()/2];
      for( int i = 0; i <pair.length; i++ ) 
      {
          pair[i] = r.substring(i*2,(i*2)+2);
      }

Before adding it to the pair array you could see if it already exists with the Arrays function .contains. 在将其添加到对数组之前,可以使用Arrays函数.contains查看它是否已经存在。 If the pair already exists then don't add it to the pair list. 如果该对已经存在,则不要将其添加到对列表中。 For example here the ab and fe pairs will not be added: 例如,此处将不添加ab和fe对:

  String r="ababtefedefe";
  String  pair[] = new String[r.length()/2];
  String currentPair = "";
  for( int i = 0; i <pair.length; i++ ) 
  {
      currentPair = r.substring(i*2,(i*2)+2);
      if(!java.util.Arrays.asList(pair).contains(currentPair))
        pair[i] = currentPair;
      System.out.println(pair[i]);
  }

I would use a Set to help me out. 我会用一套帮助我。

private String[] retrieveUniquePair(String input) {
    int dim = input.length() / 2;
    Set<String> pairs = new LinkedHashSet<>(dim);

    for (int i = 0; i <= dim; i += 2) {
        String currentPair = input.substring(i, i + 2);
        pairs.add(currentPair);
    }

    return pairs.toArray(new String[] {});
}

Edit: I post the solution I propose and the test 编辑:我发布了我建议的解决方案和测试

public class PairTest {

    @DataProvider(name = "input")
    public static Object[][] input() {
        return new Object[][] {
                {"abcd", Arrays.asList("ab", "cd")},
                {"abcde", Arrays.asList("ab", "cd")},
                {"abcdab", Arrays.asList("ab", "cd")},
                {"ababcdcd", Arrays.asList("ab", "cd")},
                {"ababtefedefe", Arrays.asList("ab", "te", "fe", "de")},
        };
    }

    @Test(dataProvider = "input")
    public void test(String input, List<String> expectedOutput) {
        String[] output = retrieveUniquePair(input);

        Assert.assertNotNull(output);
        Assert.assertEquals(output.length, expectedOutput.size());
        for (String pair : output) {
            Assert.assertTrue(expectedOutput.contains(pair));
        }
    }

    private String[] retrieveUniquePair(String input) {
        int pairNumber = input.length() / 2;
        Set<String> pairs = new LinkedHashSet<>(pairNumber);

        int endIteration = input.length();
        if (input.length() % 2 != 0) { // odd number
            endIteration--; // ignore last character
        }

        for (int i = 0; i < endIteration; i += 2) {
            String currentPair = input.substring(i, i + 2);
            pairs.add(currentPair);
        }

        return pairs.toArray(new String[pairs.size() - 1]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM