gpt4 book ai didi

java - 如何仅使用数组和循环(没有其他方法)来删除 null?

转载 作者:行者123 更新时间:2023-12-01 19:37:52 25 4
gpt4 key购买 nike

我这里有这段代码,输出带有null。我怎样才能删除它?

输入是:

hello hello i i am norman

      for (int i=0; i<words.length; i++){
boolean isUnique = true;
int freq = 0;
for (int j=0; j<words.length; j++){
if (i==j){
freq++;
continue;
}
if (words[j].equals(words[i])){
isUnique = false;
freq++;
}
}

if (isUnique){

uniquewords[i] = words[i];}

frequency[i] = freq;

}

}

输出:

null null null null am norman

最佳答案

if (uniquewords[i] != null){
System.out.println(uniquewords[i]);
}

打印时添加此检查

关于java - 如何仅使用数组和循环(没有其他方法)来删除 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59195275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com