I posted a question in the past but didn't get any response so I'm assuming my question wasn't clear. Can we define new Object[3][] without defining number of columns? Hoping following code is more readable and easy to understand.
Question: Why is retval[0] always null?
Please help. Thanks,
package YTesting1.YTesting1;
import org.testng.annotations.Test;
public class Test1 {
@Test
public void MyTest() {
Object[][] retval = new Object[1][];
String mm = "Hello";
String o = "World";
String s = "Yeaah";
(new Object[1])[0] = new Test2(mm, o, s);
retval[0] = new Object[1];
System.out.println("retval = " +retval[0]);
}
}
package YTesting1.YTesting1;
public class Test2 {
private String str1 = "";
private String str2 = "";
private String str3= "";
public Test2(String lstr1, String lstr2, String lstr3){
this.str1 = lstr1;
this.str2 = lstr2;
this.str3 = lstr3;
}
}
retval[0]
is not null, it is assigned to an empty array of Object.
It is possible to assign to retval[0]
some another array and populate it without indicating the number of columns
public static void main(String ... args) {
Object[][] retval = new Object[1][];
String mm = "Hello";
String o = "World";
String s = "Yeaah";
//(new Object[1])[0] = new Test2(mm, o, s);
retval[0] = new Object[1];
System.out.println("retval[0] = " + retval[0]);
System.out.println("retval[0][0] = " +retval[0][0]);
System.out.println("retval = " + Arrays.deepToString(retval));
retval[0] = new String[] { // 3-element string array
mm, o, s
};
System.out.println("retval[0] = " + retval[0]);
System.out.println("retval[0][0] = " +retval[0][0]);
System.out.println("retval = " + Arrays.deepToString(retval));
}
Output:
retval[0] = [Ljava.lang.Object;@5479e3f
retval[0][0] = null
retval = [[null]]
retval[0] = [Ljava.lang.String;@66133adc
retval[0][0] = Hello
retval = [[Hello, World, Yeaah]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.