Java 检查数组中的空元素
我想检查数组元素是否为null 我已经初始化了一个大小为2的Java 检查数组中的空元素,java,arrays,Java,Arrays,我想检查数组元素是否为null 我已经初始化了一个大小为2的字符串数组。我循环遍历数组并检查数组元素是否为null。如果为空,我将只向该位置添加一个字符串“a” 下面的代码将产生以下输出: 1=a 2=a 代码: 您的循环中有一些问题 for(int i = 1; i < arr.length - 1; i++) { //<---- This does not iterate over the entire array, leaving out the first and
字符串数组。我循环遍历数组并检查数组元素是否为null。如果为空,我将只向该位置添加一个字符串“a”
下面的代码将产生以下输出:
1=a
2=a
代码:
您的循环中有一些问题
for(int i = 1; i < arr.length - 1; i++) { //<---- This does not iterate over the entire array, leaving out the first and last elements
if(arr[i] == null) {
arr[i] = "a";
break; //<---- This terminates the loop entirely, if you want to stop all instructions past this one try using continue instead
}
System.out.println(i + "=" + arr[i]);
if(arr[i] == null) { //This code is unreachable as arr[i] is initialized if it was detected as null before
System.out.println(i + "= null");
}else{
System.out.println(i + "=" + arr[i]);
}
}
for(int i=1;ifor
循环之前,尝试在您希望不为null
的索引处赋值:
String[] arr = new String[3];
arr[1] = "a";
for (int i = 0; i < arr.length; i++)
{
if (arr[i] == null)
{
System.out.println(i + "=null");
}
else
{
System.out.println(i + "=" + arr[i]);
}
}
String[]arr=新字符串[3];
arr[1]=“a”;
对于(int i=0;i
这是因为break
将中断for
循环的迭代,并立即退出循环。您应该稍微更改一下设计。据我所知,您正在尝试实现(将“a”指定给遇到的第一个空元素并停止,打印数组中的所有元素)以下操作应该有效:-
public class CheckArrayElementIsNull {
public static void main(String[] args) {
String[] arr = new String[3];
boolean flag=true; //use a flag that will check if null is encountered
for(int i = 0; i < arr.length; i++) {
if(arr[i] == null && flag) { // first time flag will be true
arr[i] = "a";
flag=false; //set flag to false , to avoid further assignment of 'a' to null values
}
System.out.println(i + "="+ arr[i]); //print array
}
}
}
公共类checkArrayElementsNull{
公共静态void main(字符串[]args){
字符串[]arr=新字符串[3];
boolean flag=true;//使用将检查是否遇到null的标志
对于(int i=0;i
在分配给它之前,您应该检查并打印空值,否则当您第二次检查它时,该元素不会为空。为什么从1开始而不是从0开始?好的,也许我应该从0开始。抱歉。编辑仍然会将数组中的所有值分配给a
。据我所知,问题是de预期效果:将所有空值设置为“a” instead@Stefan不是所有的值,只有那些设置为null的值才是数组中找到a的第一个null值。@tz reur哦,只有第一个?修复了:)这不会导致System.out.println中出现NullPointerException
异常吗如果数组包含多个null
元素?@dragondraik不会,它只会打印null,这是因为我正在传递一个String
类型的对象(其值为null),因此编译器知道要调用哪个toString()
方法。当我编写System.out.println(null)
时,会发生NPE,因为在这种情况下,编译器无法推断要调用哪个toString()
方法。
for(int i = 1; i < arr.length - 1; i++) { //<---- This does not iterate over the entire array, leaving out the first and last elements
if(arr[i] == null) {
arr[i] = "a";
break; //<---- This terminates the loop entirely, if you want to stop all instructions past this one try using continue instead
}
System.out.println(i + "=" + arr[i]);
if(arr[i] == null) { //This code is unreachable as arr[i] is initialized if it was detected as null before
System.out.println(i + "= null");
}else{
System.out.println(i + "=" + arr[i]);
}
}
for(int i = 0; i < arr.length; i++) {
if(arr[i] == null) {
arr[i] = "a";
System.out.println(i + "= null");
break;
}
System.out.println(i + "=" + arr[i]);
}
String[] arr = new String[3];
arr[1] = "a";
for (int i = 0; i < arr.length; i++)
{
if (arr[i] == null)
{
System.out.println(i + "=null");
}
else
{
System.out.println(i + "=" + arr[i]);
}
}
public class CheckArrayElementIsNull {
public static void main(String[] args) {
String[] arr = new String[3];
boolean flag=true; //use a flag that will check if null is encountered
for(int i = 0; i < arr.length; i++) {
if(arr[i] == null && flag) { // first time flag will be true
arr[i] = "a";
flag=false; //set flag to false , to avoid further assignment of 'a' to null values
}
System.out.println(i + "="+ arr[i]); //print array
}
}
}