发疯:为什么成员 class 中的数组为零
Going nuts: Why is array in member class zero
我不知道这里发生了什么。我做了一个简单的应用程序,它有一个成员 class。
这是我的代码,其行为符合预期。这里没什么好看的。 main class 使用构造函数初始化成员 class 并调用成员方法。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int array[] = null;
// Init member class
subClass = new SubClass();
subClass.doSomething();
}
会员代码class:
package com.example.test;
public class SubClass {
private int[] array;
public SubClass(){
if(array==null){
array = new int[10];
}
}
public void doSomething(){
if(array == null){
// We don't get here, which is good.
}
}
}
但现在我想向成员 class 传递一个数组,例如来自 savedInstanceState 的数组。为了使示例简洁明了,我只传递了一个空数组引用。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int[] array = null;
subClass = new SubClass(array);
subClass.doSomething();
}
成员class:
package com.example.test;
public class SubClass {
private int[] array;
public SubClass(int[] array){
this.array = array;
// Whatever was passed, I want to make sure we have an array to work with.
if(array==null){
// Yes, it is null so init it.
array = new int[10];
// FROM HERE IT SHOULD BE AN int[10] array AND NOT NULL IN THIS CLASS
// NO MATTER WHAT THE CALLING APPLICATION DOES WITH IT'S PRIVATE
// VARIABLE (no, not parts ;))
}
}
public void doSomething(){
// and now, array should be of 10 length but it isn't!
if(array == null){
// We do get here, which is wrong!
System.out.println("array == null in doSomething");
}
}
}
如果我确实传递了一个有效的数组,比如 array = new int[1] 并且在构造函数中忽略传递的内容并且总是初始化为 array = new int[10],在 doSomething 方法中它是一个 int [1] 再次排列!
谢谢。
您正在设置数组的值,而您需要设置 this.array。如果你只设置数组,旧值是空的,所以它无法复制到 class 成员变量。
空引用无效,如果变量访问同一地址,则引用仅在变量之间复制,在这种情况下它们不是,因为在初始化数组之前两者都是空的。如果您在将数组分配给 this.array 之前初始化了数组,那么它们将具有相同的引用并且您可以互换使用它们直到构造函数返回。
if(array==null){
// Yes, it is null so init it.
array = new int[10];
// FROM HERE IT SHOULD BE AN int[10] array AND NOT NULL IN THIS CLASS
// NO MATTER WHAT THE CALLING APPLICATION DOES WITH IT'S PRIVATE
// VARIABLE (no, not parts ;))
}
您分配的是 array
而不是 this.array
。
array
在这种情况下是方法局部数组而不是 class 变量。
所以:
if(array==null){
array = new int[10];
System.out.println(array);
System.out.println(this.array);
}
会打印
Array[..]
null
稍后在 doSomething 中引用它时,您正在引用 this.array
。
我不知道这里发生了什么。我做了一个简单的应用程序,它有一个成员 class。
这是我的代码,其行为符合预期。这里没什么好看的。 main class 使用构造函数初始化成员 class 并调用成员方法。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int array[] = null;
// Init member class
subClass = new SubClass();
subClass.doSomething();
}
会员代码class:
package com.example.test;
public class SubClass {
private int[] array;
public SubClass(){
if(array==null){
array = new int[10];
}
}
public void doSomething(){
if(array == null){
// We don't get here, which is good.
}
}
}
但现在我想向成员 class 传递一个数组,例如来自 savedInstanceState 的数组。为了使示例简洁明了,我只传递了一个空数组引用。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int[] array = null;
subClass = new SubClass(array);
subClass.doSomething();
}
成员class:
package com.example.test;
public class SubClass {
private int[] array;
public SubClass(int[] array){
this.array = array;
// Whatever was passed, I want to make sure we have an array to work with.
if(array==null){
// Yes, it is null so init it.
array = new int[10];
// FROM HERE IT SHOULD BE AN int[10] array AND NOT NULL IN THIS CLASS
// NO MATTER WHAT THE CALLING APPLICATION DOES WITH IT'S PRIVATE
// VARIABLE (no, not parts ;))
}
}
public void doSomething(){
// and now, array should be of 10 length but it isn't!
if(array == null){
// We do get here, which is wrong!
System.out.println("array == null in doSomething");
}
}
}
如果我确实传递了一个有效的数组,比如 array = new int[1] 并且在构造函数中忽略传递的内容并且总是初始化为 array = new int[10],在 doSomething 方法中它是一个 int [1] 再次排列!
谢谢。
您正在设置数组的值,而您需要设置 this.array。如果你只设置数组,旧值是空的,所以它无法复制到 class 成员变量。
空引用无效,如果变量访问同一地址,则引用仅在变量之间复制,在这种情况下它们不是,因为在初始化数组之前两者都是空的。如果您在将数组分配给 this.array 之前初始化了数组,那么它们将具有相同的引用并且您可以互换使用它们直到构造函数返回。
if(array==null){
// Yes, it is null so init it.
array = new int[10];
// FROM HERE IT SHOULD BE AN int[10] array AND NOT NULL IN THIS CLASS
// NO MATTER WHAT THE CALLING APPLICATION DOES WITH IT'S PRIVATE
// VARIABLE (no, not parts ;))
}
您分配的是 array
而不是 this.array
。
array
在这种情况下是方法局部数组而不是 class 变量。
所以:
if(array==null){
array = new int[10];
System.out.println(array);
System.out.println(this.array);
}
会打印
Array[..]
null
稍后在 doSomething 中引用它时,您正在引用 this.array
。