再次需要帮助。对不起。如何从数组中删除空值?这是我到目前为止所得到的。
int unikCount = 0;
String c = " ";
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (tempAry[i].equals(tempAry[j])) {
unikCount++;
}
if (unikCount > 1) {
tempAry[j] = c;
unikCount = 1;
}
}
unikCount = 0;
}
for (i = 0; i < a.length; i++) {
if (tempAry[i] != c) {
unikCount++;
}
}
System.out.println(unikCount);
for (int i = 0; i < a.length; i++) {
for (int j = 1; j < a.length; j++) {
if (tempAry[i].equals(tempAry[j])) {
if (tempAry[i] == c) {
count++;
if (tempAry[j] != c) {
count++;
tempAry[j] = tempAry[i];
}
}
}
}
}
count = 0;
for (int i = 0; i < a.length; i++) {
System.out.println(tempAry[i]);
}
*删除部分位于“System.out.println(unikCount)”之后。感谢您即将提供的帮助。顺便说一句,不能使用 hash 和 arraylist。
最佳答案
您可以像这样检查null
:
if ( null == someObject )
{
// do things
}
无法从数组中删除元素并使其自动收缩。您必须使用临时数组来保存值,创建新大小的数组并传输所有这些项目。
更有效的方法是使用 List .
关于java - 如何从数组中删除空值/元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19192173/