Java String数组:如何检查数组是否有值?

时间:2021-03-08 07:29:33

I'm making a program in java that will use a string array say:

我在java中创建一个使用字符串数组的程序说:

String[] category = new String[46];

Then I will check if the array in a for loop if it already has a value,

然后我会检查一个for循环中的数组是否已经有一个值,

for(int checking = 21; checking <= 45 ;checking++) {
    if(category[checking]=INSERT_HERE) {
        textArea += category[checking] + "\n";
    }
}

What do I put in INSERT_HERE? Note: textArea is a named JTextArea.

我在INSERT_HERE中放了什么?注意:textArea是一个名为JTextArea的。

5 个解决方案

#1


1  

If you are making a check if the value is not null, then use

如果要检查值是否为null,则使用

if(category[checking]!=null)

And if you are making a check for some particular value, then

如果你正在检查一些特定的价值,那么

if(category[checking].equals(PARTICULAR_VALUE))

PS: '=' is for assignment, you should use '==' for comparison.

PS:'='用于分配,您应该使用'=='进行比较。

#2


0  

u have to checkout if it is not null use like this:

你必须检查它是否不是null使用如下:

 if(category[checking] != null) // will check all filled values only

#3


0  

When you define your array as

将数组定义为

String[] category=new String[46];

you allocate 46 reference slots in memory for your array. These slots are initially null, so when you need to do a comparison like the one you asked, you need to check against null.

在内存中为阵列分配46个参考插槽。这些插槽最初为null,因此当您需要像您询问的那样进行比较时,需要检查null。

...
if(category[checking] != null)
...

#4


0  

You could try something like that in your code:

您可以在代码中尝试类似的东西:

for(int checking=21;checking<=45;checking++) {
    if(category[checking] != null) {
        textArea+=category[checking] +"\n";
    }
}

Or like that:

或者像那样:

for(int checking=21;checking<=45;checking++) {
    if(category[checking] != "") {
        textArea+=category[checking] +"\n";
    }
}

#5


0  

for(int checking=21;checking<=45;checking++) {
    if(category[checking] != null || category[checking] != "") {
       textArea += category[checking] +"\n";
    }
}

#1


1  

If you are making a check if the value is not null, then use

如果要检查值是否为null,则使用

if(category[checking]!=null)

And if you are making a check for some particular value, then

如果你正在检查一些特定的价值,那么

if(category[checking].equals(PARTICULAR_VALUE))

PS: '=' is for assignment, you should use '==' for comparison.

PS:'='用于分配,您应该使用'=='进行比较。

#2


0  

u have to checkout if it is not null use like this:

你必须检查它是否不是null使用如下:

 if(category[checking] != null) // will check all filled values only

#3


0  

When you define your array as

将数组定义为

String[] category=new String[46];

you allocate 46 reference slots in memory for your array. These slots are initially null, so when you need to do a comparison like the one you asked, you need to check against null.

在内存中为阵列分配46个参考插槽。这些插槽最初为null,因此当您需要像您询问的那样进行比较时,需要检查null。

...
if(category[checking] != null)
...

#4


0  

You could try something like that in your code:

您可以在代码中尝试类似的东西:

for(int checking=21;checking<=45;checking++) {
    if(category[checking] != null) {
        textArea+=category[checking] +"\n";
    }
}

Or like that:

或者像那样:

for(int checking=21;checking<=45;checking++) {
    if(category[checking] != "") {
        textArea+=category[checking] +"\n";
    }
}

#5


0  

for(int checking=21;checking<=45;checking++) {
    if(category[checking] != null || category[checking] != "") {
       textArea += category[checking] +"\n";
    }
}