Java 使用最少的字符串方法计算字符串数组中出现的单词数
请帮助我找到字符串数组中单词计数的逻辑。不使用字符串方法将是一个加分 我一直在研究的代码:Java 使用最少的字符串方法计算字符串数组中出现的单词数,java,arrays,string,Java,Arrays,String,请帮助我找到字符串数组中单词计数的逻辑。不使用字符串方法将是一个加分 我一直在研究的代码: class Scratch { public static void main(String args[]) { String[] str = { "sup", "aisu", "sup", "aisu", "sandy", "sandu", "vijay", "gani", "sup", "sup", "gani", "sandu" };
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);
}
}
这是你的代码应该是怎样的,在某种程度上
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];
}
}
}
publicstaticvoidmain(字符串[]args){
字符串[]str={“sup”、“aisu”、“sup”、“aisu”、“sandy”、“sandu”、“vijay”、“gani”、“sup”、“sup”、“gani”、“sandu”};
字符串[]alreadyValidate=新字符串[str.length];
int i=0,j=0,count=0;
System.out.println(“数组大小为”+str.length);
System.out.println(“数组元素为”);
对于(i=0;i,您只需在选中元素之前检查数组中是否存在单词:
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++;
}
}
String[]a=。。。;
整数计数=0;
for(int i=0;i
用于(j=0;j
你所说的“没有字符串函数”是什么意思?我们如何在没有任何函数的情况下检查字符串的内容(我假设你指的是方法)?“计算字数”是什么意思?你的例子的结果应该是什么?你的意思是你需要计算唯一的单词?你已经在使用array.length
,你还想要什么?Pshemo,esin88:谢谢你的回答。我需要输出,例如我的array={“a”,“b”,“a”},a:2的计数,b:1的计数str[j]==str[k]
不会做你认为它会做的事。请看。如果我想要输出为sup:4的计数等等,那么必须做什么修改?对不起,我不清楚这个问题。你能详细说明你的要求吗?@Anida Bhattacharyya:我的要求是找到字符串数组中存在的单词的计数。例如,我有一个数组a[]={“a”、“a”、“b”、“c”};我希望得到的结果是a:2、b:1、c:1的计数。我希望这个结果不使用集合,如果可能的话,不使用字符串方法或最小限度地使用字符串方法。您只需更改最后一个`System.out.println(“…+count+”表示word…+str[I]”)
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++;
}
}
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);
}