二维为空的2D数组如何工作? [英] How do 2D arrays with the second dimension empty work?
本文介绍了二维为空的2D数组如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法理解以下代码:
String [][] abc = new String [4][];
这是否会创建2行的4行数组,但每一行都是空的?每行都有初始值吗?如果每一行都有特定数量的行,那么初始值是什么?
Would this create the 2d array of 4 rows but each row is empty? Is an initial value given for each row? If each row gets a specific number of rows, what would the initial value be?
推荐答案
这会创建2行的4行数组,但每一行都是空吗?
Would this create the 2d array of 4 rows but each row is empty?
它创建4行2D数组.
每一行都有初始值吗?
Is an initial value given for each row?
默认情况下,每行为 null
.
如果每一行都有特定数量的行,则初始行将是什么值得吗?
If each row gets a specific number of rows, what would the initial value be?
每行可以有不同数量的元素
Each row can have a different number of elements
示例程序:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[][] abc = new String[4][];
for (String[] s : abc)
System.out.println(s);
// You can initialize in this way
abc[0] = new String[] { "Sam", "John", "Chris" };
String[] str = { "ab", "bc", "cd", "ef" };
// You can initialize also in this way
abc[1] = str;
// You can initialize also in this way
abc[2] = new String[2];
abc[2][0] = "Hello";
abc[2][1] = "World";
abc[3] = new String[4];
for (String[] s : abc)
System.out.println(Arrays.toString(s));
}
}
输出:
null
null
null
null
[Sam, John, Chris]
[ab, bc, cd, ef]
[Hello, World]
[null, null, null, null]
这篇关于二维为空的2D数组如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文