【发布时间】: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。