【问题标题】:Text to string array and delete duplicates文本到字符串数组并删除重复项
【发布时间】:2022-01-19 08:04:33
【问题描述】:

该程序的想法是它从扫描仪获取文本除以空格。

我需要编写一个方法来从文本创建一个数组,删除重复项并返回一个只使用一次且没有重复项的单词数组。

我不知道如何创建一个新的唯一单词数组。仅使用没有 HashSet 等的简单和基本构造。*

例如:

a b a b c a b d

结果:

c d
public static String Dublicate(String text) { 
    String[] dublic = text.split(" ");
    String result="";
    for (int i = 0; i < dublic.length; i++) {
        for (int j = i + 1; j < dublic.length; j++)
            if (dublic[i].equals(dublic[j]))
                dublic[j] = "delete";
    }
    for (String s: dublic) {
        if (s !="delete") {
            result =result + s + " ";  
        }
    }
    return result;
}

【问题讨论】:

  • 欢迎来到 Stack Overflow!您似乎在要求某人为您编写一些代码。 Stack Overflow 是一个问答网站,而不是代码编写服务。请see here学习如何写出有效的问题

标签: java arrays string


【解决方案1】:

按空间分割

按空格分割可以使用split()方法&可以在参数中传递空格字符串("")。

String[] texts = text.split(" ");

删除重复的元素

如果我们可以使用 java 1.8 或高于 1.8,我们可以使用流 API 来获取不同的元素,例如。

Arrays.stream(texts).distinct().toArray(String[]::new);

或者如果我们需要在 java 1.7 中实现它,我们可以使用 HashSet 来获取不同的元素,例如。

String[] distinctElements = new HashSet<String>(Arrays.asList(texts)).toArray(new String[0]);

最终的源码可以是这样的:

public static String[] textToArray1_7(String text) {
        //split by space
        String[] texts = text.split(" ");
        //Distinct value
        return Arrays.stream(texts).distinct().toArray(String[]::new);
    }

    public static String[] textToArray1_8(String text) {
        //split by space
        String[] texts = text.split(" ");
        //Distinct value
        return  new HashSet<String>(Arrays.asList(texts)).toArray(new String[0]);
    }

如果有任何进一步的问题,可以要求更多的澄清。

【讨论】:

  • 你误读了这个问题:目标不是返回不同的元素,而是只返回只出现一次的元素。
【解决方案2】:

您忘记将第 i 个元素标记为重复,以防万一。在下面的代码中查看我的 cmets

public static String Dublicate(String text) {
    String[] dublic = text.split(" ");
    String result="";
    for (int i=0; i<dublic.length; i++){
        if (dublic[i].equals("delete")) { // Minor optimization: 
            // skip elements that are already marked
            continue;
        }
        boolean isDub = false;   // we need to track i-th element
        for(int j=i+1; j<dublic.length; j++) {
            if (dublic[i].equals(dublic[j])) {
                dublic[j] = "delete";
                isDub = true;   // i-th element is also a duplicate...
            }
        }
        if (isDub) {
            dublic[i] = "delete";  // ...so you should also mark it
        }
    }
    for(String s: dublic){
        if(!s.equals("delete")) { // for strings you should use "!equals" instead of "!="
            result = result + s + " ";
        }
    }
    return result;
}

附:如果原始文本包含“delete”,则结果将不正确,因为您使用“delete”作为保留标记字

【讨论】:

    【解决方案3】:

    如果需要返回唯一字符串数组,那么必须压缩分割输入后的初始字符串数组以排除无效值,并且需要返回一个较小的副本:

    public static String[] uniques(String text) { 
        String[] words = text.split(" ");
    
        int p = 0; // index/counter of unique elements
        for (int i = 0; i < words.length; i++) {
            String curr = words[i];
            if (null == curr) {
                continue;
            }
            boolean dupFound = false;
            for (int j = i + 1; j < words.length; j++) {
                if (null == words[j]) {
                    continue;
                }
                if (curr.equals(words[j])) {
                    words[j] = null;
                    dupFound = true;
                }
            }
            if (dupFound) {
                words[i] = null;
            } else {
                words[p++] = words[i]; // shift unique elements to the start of array
            }
        }
        
        return Arrays.copyOf(words, p);
    }
    

    如果返回的是唯一字符串数组,可以使用String::join方便的转换成String,如下图测试。 测试:

    System.out.println(Arrays.toString(uniques("a b a b c a b d")));
    System.out.println(String.join(" ", uniques("a b a b c a b d")));
    

    输出

    [c, d]
    c d
    

    【讨论】:

      猜你喜欢
      • 2017-12-30
      • 1970-01-01
      • 2014-04-29
      • 2012-09-15
      • 1970-01-01
      • 2021-03-21
      • 2019-06-14
      • 1970-01-01
      • 2012-05-09
      相关资源
      最近更新 更多