Java 将 NULL 值显式设置为数组元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18296766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Explicitly setting NULL value to an array element
提问by UnderDog
Can someone tell me why am I getting compilation error for explicitly setting NULL value to an array element ?
有人能告诉我为什么我会因为将 NULL 值显式设置为数组元素而出现编译错误吗?
class array_1
{
public static void main(String args[])
{
int[] a = new int[5];
a[0]=1;
a[2]='a';
a[3]=null; //Compiler complains here
for(int i : a) System.out.println(i);
}
}
I am assuming because its an int array and the literal value allowed is 0 and not NULL. Am I right ?
我假设因为它是一个 int 数组,并且允许的文字值为 0 而不是 NULL。我对吗 ?
回答by rocketboy
Your array
你的数组
int[] a
is of primitive type int
. Primitives cannot have a null
value but instead have a default value of 0. Only objects in java can have null as a value
. Primitives include: byte,short,char,int,long,float,double.
是原始类型int
。原语不能有null
值,而是有默认值 0。只有 java 中的对象可以将 null 作为value
。原语包括:byte,short,char,int,long,float,double.
回答by Doorknob
I am assuming because its an int array and the literal value allowed is 0 and not NULL. Am I right ?
我假设因为它是一个 int 数组,并且允许的文字值为 0 而不是 NULL。我对吗 ?
Yes.
是的。
If you want to be able to use null
, make it an Integer[]
. Integer
s are objects, which can be set to null
, unlike primitives (int
, char
, etc.).
如果您希望能够使用null
,请将其设为Integer[]
. Integer
s为对象,它可以被设置为null
,与基元(int
,char
等)。
回答by chrylis -cautiouslyoptimistic-
Correct. int
is a primitive type, which means that it contains an explicit value (a number from -2^31 to 2^31-1), and not a reference, so it can't be null
. If you really need null
values, use Integer
.
正确的。int
是原始类型,这意味着它包含显式值(从 -2^31 到 2^31-1 的数字),而不是引用,因此它不能是null
. 如果您确实需要null
值,请使用Integer
.
回答by Bohemian
int
is a primitivetype, which can't be null
- it must have a value.
int
是原始类型,不能是null
- 它必须有一个值。
The only option is to set it to a value you can treatlike "null", for example -1
.
唯一的选择是将其设置为您可以像“null”一样对待的值,例如-1
。
回答by Ruchira Gayan Ranaweera
This is a int
array. default value of int
isn't null an default value is 0
, If this is an Integer
array then you can set null
.
这是一个int
数组。默认值int
不是 null 默认值为0
,如果这是一个Integer
数组,那么您可以设置null
.
回答by Rudra
int[] a = new int[5];
The declared array is of type int which is a primitive type like byte, short, long, float, double, char and boolean. Primitives cannot have a null value but instead have their default value as given below
声明的数组是 int 类型,它是一个基本类型,如 byte、short、long、float、double、char 和 boolean。基元不能有空值,而是有它们的默认值,如下所示
byte = 0;
short = 0;
int = 0;
long = 0l;
float = 0.0f
double = 0.0d
boolean = false;
The range formula for the primitive types is mentioned below. Primitives can accept only values falling into this range.
下面提到了基本类型的范围公式。原语只能接受落入此范围内的值。
-2^(N - 1) to 2^(N - 1)-1
where N stands for no. of bits that each primitive type takes
Only objects in java can have null as a value. If anyways you want so, better use wrapper classes like
只有 java 中的对象可以将 null 作为值。如果无论如何你想要这样,最好使用包装类
Byte, Short, Integer, Long, Character, Boolean, etc.
回答by rohit agrawal
Declare the array as: Integer[] a = new Integer[5];
将数组声明为:Integer[] a = new Integer[5];
The rest should work as is.
其余的应该按原样工作。
回答by Pedro Salas
An Integer array elements are value types (Int type) so they store values assigned to them in memory locations. If you want to fake a null value, you could try to assign a -1 value to the elements.
整数数组元素是值类型(Int 类型),因此它们将分配给它们的值存储在内存位置。如果要伪造空值,可以尝试为元素分配 -1 值。
Try something like this...
尝试这样的事情......
public static void main(String[]args){
int a[][]={{4,3,2,1},{0,-1,-1,-1},{0,-1,-1,-1}};
printStatus(a);
procesar(a);
printStatus(a);
}
public static void procesar (int a[][])
{
int temp, tope0, tope1, tope2;
tope0 = ((a[0].length)-1);
tope1 = 0;
tope2 = 0;
while(a[0][tope0] >= 0){
if(a[2][tope2]==0){
System.out.println(a[2][tope2]);
temp=a[0][tope0];
a[2][tope2] = temp;
a[0][tope0] = 0;
tope0= tope0 -1;
tope2 = tope2 +1 ;
System.out.println(a[0][tope0]+ " "+a[1][tope1] +" "+ a[2][tope2 -1] );
printStatus(a);
}
System.out.println(a[0][tope0]+ " "+ a[2][(tope2-1)] );
if((a[2][(tope2 -1)]> a[0][tope0])){
temp=a[0][tope0];
a[2][tope2] = temp;
a[0][tope0] = 0;
tope0= tope0 -1;
tope2 = tope2 +1 ;
System.out.println(a[0][tope0]+ " "+a[1][tope1] +" "+ a[2][tope2 -1] );
printStatus(a);
}
if(a[1][tope1]==0){
System.out.println(a[1][tope1]);
temp = a[0][tope0];
a[1][tope1]= temp;
a[0][tope0]= 0;
tope0= tope0 -1;
tope1 = tope1 +1;
System.out.println(a[0][tope0]+ " "+a[1][tope1 - 1] +" "+ a[2][tope2 -1] );
printStatus(a);
}
System.out.println(a[0][tope0]+ " "+ a[1][(tope1-1)] );
if(a[1][(tope1-1)]> a[0][tope0]){
temp = a[0][tope0];
a[1][tope1]= temp;
a[0][tope0]= 0;
tope0= tope0 -1;
tope1 = tope1 +1;
System.out.println(a[0][tope0]+ " "+a[1][tope1 - 1] +" "+ a[2][tope2 -1] );
printStatus(a);
}