【问题标题】:Count occurrences of words in a String array with minimal use of string methods [closed]使用最少的字符串方法计算字符串数组中单词的出现次数[关闭]
【发布时间】:2015-03-06 20:45:34
【问题描述】:

请帮助我找出字符串数组中字数的逻辑。不使用 String 方法将是一个加分点。

我一直在处理的代码:

class Scratch
{
    public static void main(String args[])
    {
        String[] str = { "sup", "aisu", "sup", "aisu", "sandy",
            "sandu", "vijay", "gani", "sup", "sup", "gani", "sandu" };
        int i = 0, j = 0, k = 0, count = 0;
        System.out.println("The array size is" + str.length);
        System.out.println("The array elements are");
        for (i = 0; i < str.length; i++)
            System.out.println(str[i]);

        System.out.println("Comparison");
        for (j = 0; j < str.length; j++)
        {
            for (k = 1; k < str.length - 1; k++)
            {
                if (str[j] == str[k])
                {
                    count++;
                    break;
                }
            }
        }
        System.out.println("," + count);
    }
}

请注意逻辑应该包含集合概念。

输出要求

Count of sup   is : 4
Count of aisu  is : 2
Count of sandy is : 1
Count of sandu is : 2
Count of vijay is : 1
Count of gani  is : 2

【问题讨论】:

  • “没有字符串函数”是什么意思?我们如何在没有任何功能的情况下检查字符串的内容(我假设您的意思是方法)?另外,“计算字数”是什么意思?您的示例应该得到什么结果?
  • 你的意思是你需要计算唯一的单词?
  • 您已经在使用array.length,您还想要什么?
  • Pshemo, esin88:谢谢你的回复。我需要输出,例如我的数组 ={"a","b","a"} ,a 计数:2 ,b 计数:1
  • str[j]==str[k] 不会像您认为的那样做。见how to compare strings in Java

标签: java arrays string


【解决方案1】:

这是你的代码应该是怎样的,以一种方式......

public static void main(String[] args){

    String[] str ={"sup","aisu", "sup","aisu","sandy","sandu","vijay","gani","sup","sup","gani","sandu"};
    String[] alreadyValidated = new String[str.length];
      int i=0,j=0,count=0;
      System.out.println("The array size is"+str.length);
      System.out.println("The array elements are");
      for( i=0;i<str.length;i++)
                for(j=0;j<str.length;j++)
      {
            boolean skipCheck = false;
            for (String string : alreadyValidated) {
                if(str[i] == string) {
                    skipCheck = true;
                    break;
                }
            }
            if(!skipCheck) {
                System.out.println("Checking for word..."+str[i]);
                for(j=1;j<str.length-1;j++)
                {
                     if(str[i]==str[j])
                     {
                         count++;
                      }
                }
                System.out.println("The count is ..."+count+ " for word..."+str[i]);
                count = 0;
                alreadyValidated[i] = str[i];
            }
       }

}

【讨论】:

  • 如果我希望输出为 sup 的计数,必须进行哪些更改:4 等等
  • 对不起,我不清楚这个问题。您能详细说明您的要求吗?
  • @Aninda Bhattacharyya:我的要求是找到字符串数组中存在的单词的计数。例如我有一个数组 a[]={"a","a","b","c"};我想将结果作为 a 的计数:2,b 的计数:1,c 的计数:1。我想要这个结果而不使用集合,如果可能的话,不使用字符串方法或最少使用字符串方法
  • 您只需要更改最后一个 `System.out.println("The count is ..."+count+ " for word..."+str[i]);` 为此。还需要帮助吗?
  • 我得到的输出是 sup 的计数:3 这是打印 3 次,aisu 的计数:2 是重复 3 两次,但我的要求是 sup 的计数:4,aisu 的计数:2
【解决方案2】:

您可以简单地检查该单词是否存在于被检查元素之前的数组中:

String[] a = ...;
int count = 0;
for (int i = 0; i < a.length; i++) {
    boolean existsBefore = false;
    for (int j = 0; j != i; j++) {
        if (a[i].equals(a[j])) {
            existsBefore = true;
            break;
        }
    }
    if (!existsBefore) {
        count++;
    }
}

【讨论】:

    【解决方案3】:
    for (j = 0; j < str.length - 1; j++) {
      for (k = 1; k < str.length; k++) { 
        if (str[j] == str[k]) { 
          count++; 
        }
      }
      System.out.println("Num of words " + str[j] + ":" + count);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多