如何在String排序数组中计算等于字符串

时间:2015-07-22 11:20:23

标签: java arrays string

我想在数组String中计算相同的单词并打印此整数数组。例如:

输入:String [] s= {"be" , "be" , "to" , "onto","onto","onto"}

  

输出2,1,3

输入:String words[] = {"be", "be", "not", "or", "to", "to", "to"}

  

输出:2,1,1,3

我的代码:

    //O(n)
    public static int [] MaxNumber(String [] arr)
    {
        int [] Number_arr=new int[11];
        int count=1;
        int j=0;
        int k=0;
        for(int i = 0; i<arr.length-1; i++)
        {
            if(arr[i].equals(arr[i+1]))
                count++;
            else{
                Number_arr[j]=count;
                j++;
                count=1;
            }

        }

        return Number_arr;
    }

我的输入:String [] Sarr= {"be" , "be", "not","not","not", "or","to","to","to"};

  

我的输出错误:2,3,1

     

正确的输出是:2,3,1,3

我该怎么做?

6 个答案:

答案 0 :(得分:4)

您忘了添加最后一个相等字符串序列的计数:

for(int i = 0; i<arr.length-1; i++)
{
    if(arr[i].equals(arr[i+1]))
        count++;
    else{
        Number_arr[j]=count;
        j++;
        count=1;
    }

}
Number_arr[j]=count; // added

答案 1 :(得分:1)

 public static int [] MaxNumber(String [] arr)
    {
        int [] Number_arr=new int[11];
        int count=1;
        int j=0;
        int k=0;
        for(int i = 0; i<arr.length-1; i++)
        {
            if(arr[i].equals(arr[i+1]))
                count++;
            else{
                Number_arr[j]=count;
                j++;
                count=1;
            }

        }

        Number_arr[j]=count;   // *added line*
        for (int i = 0; i < Number_arr.length; i++) {
            System.out.print(Number_arr[i]+"  ");
        }

        return Number_arr;
    }

似乎最后一次出现没有正确添加。看看额外添加的行。

答案 2 :(得分:1)

您的输入是:

    String [] Sarr= {"be" , "be", "not","not","not", "or","to","to","to"};

您可以看到Sarr[6]Sarr[7]Sarr[8]的值相同。假设以下值:

    i = 6; j = 3; count = 1;

现在调试你的代码,你会注意到:

    i = 7; j = 3; count = 2;
    i = 8; j = 3; count = 3;

此时i = 8测试条件i<arr.length-1;在下一次迭代时返回false,因此最新的count值将永远不会添加到Number_arr,因为if-else部分不会执行。将以下语句放在for循环的末尾,以确保count的最后一个值也添加到Number_arr

    Number_arr[j]=count;

修改后的代码是:

    public static int [] MaxNumber(String [] arr)
    {
            int [] Number_arr=new int[11];
            int count=1;
            int j=0;
            int k=0;
            for(int i = 0; i<arr.length-1; i++)
            {
                if(arr[i].equals(arr[i+1]))
                    count++;
                else
                {
                    Number_arr[j]=count;
                    j++;
                    count=1;
                }
            }
            Number_arr[j]=count;
            return Number_arr;
    }

希望这有帮助。

答案 3 :(得分:0)

public static int [] MaxNumber(String [] arr)
{
String [] arr={"be" , "be" , "to" , "onto","onto","onto"};
int [] Number_arr=new int[11];
int count=1;
int j=0;
int k=0;
for(int i = 0; i<arr.length-1; i++)
{
    if(arr[i].equals(arr[i+1]))
        count++;
    else{
        Number_arr[++j]=count;
        count=1;
    }
}
Number_arr[++j]=count;
return Number_arr;
}

我添加了一行代码....试图保持你的代码原样...... 我希望这有帮助

答案 4 :(得分:0)

你已经得到了解决你正在处理具有连续重复单词的数组的情况的答案。但是,数组中的单词是否总是连续的?如果没有,你可能想重新考虑你的逻辑。

话虽如此,使用Java 8中的流使得这一点变得相当微不足道。可能不是O(n),但它完成了工作。我们从数组中获取每个不同的单词,然后对它们进行过滤计数。

public static void main(String[] args) throws Exception {
    String [] Sarr= {"be", "be", "not", "not", "not", "or", "to", "to", "to", "be", "not"};
    Arrays.stream(Sarr).distinct()
            .forEach(word -> System.out.println(word + ": " + Arrays.stream(Sarr).filter(w -> w.equals(word)).count()));
}

结果:

be: 3
not: 4
or: 1
to: 3

如果您想将计数结果存储到数组中,请尝试以下操作:

public static void main(String[] args) throws Exception {
    String [] Sarr= {"be", "be", "not", "not", "not", "or", "to", "to", "to", "be", "not"};
    String[] distincts = Arrays.stream(Sarr).distinct().toArray(size -> new String[size]);

    long[] wordCount = new long[distincts.length];
    for (int i = 0; i < wordCount.length; i++) {
        final String distinct = distincts[i];
        wordCount[i] = Arrays.stream(Sarr).filter(word -> word.equals(distinct)).count();
    }
    System.out.println(Arrays.toString(wordCount));
}

结果:

[3, 4, 1, 3]

答案 5 :(得分:0)

使用Java-Stream-API的另一个简单解决方案是:

    Stream<String> stream= Stream.of("a","a","b","c","c");
    Map<String, Long> counter = stream.collect(Collectors.groupingBy(String::new, Collectors.counting()));
    for (Entry<String, Long> count: counter.entrySet()) {
        System.out.println(count.getKey() + " : " + count.getValue());
    }

结果:

a : 2
b : 1
c : 2