[英]Java String array: how do I check if the array has a value?
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? 我在INSERT_HERE中放什么? Note: textArea is a named JTextArea
. 注意:textArea是一个名为JTextArea
。
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:“ =”用于分配,您应使用“ ==”进行比较。
for(int checking=21;checking<=45;checking++) {
if(category[checking] != null || category[checking] != "") {
textArea += category[checking] +"\n";
}
}
您必须检出它是否不为null,如下所示:
if(category[checking] != null) // will check all filled values only
When you define your array as 当您将数组定义为
String[] category=new String[46];
you allocate 46 reference slots in memory for your array. 您可以为阵列在内存中分配46个参考插槽。 These slots are initially null
, so when you need to do a comparison like the one you asked, you need to check against null
. 这些插槽最初为null
,因此当您需要像您要求的那样进行比较时,需要检查null
。
...
if(category[checking] != null)
...
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";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.