int arr[][] = new int[2][];
System.out.println(Arrays.toString(arr));
上面的代碼片段在運行時創建了一個包含 2 個元素的新二維陣列,每個元素本身就是一個陣列。但是由于我在陣列初始化期間沒有指定第二對 [] ,所以 print 陳述句給了我[null, null]。
我的問題是為什么我只得到[]在運行下面的代碼時
ArrayList<ArrayList<Integer>> arrList = new ArrayList<ArrayList<Integer>>(2);
System.out.println(arrList);
據我說,因為我創建了一個初始容量 = 2 的 ArrayList,所以當我列印 ArrayList 時我應該得到[null,null],類似于我列印上述陣列時得到的結果。
我是Java新手,所以任何幫助將不勝感激。
uj5u.com熱心網友回復:
我自己很新,但我會盡力回答這個問題。
在第一行列印期間:
Arrays.toString 方法僅迭代外部陣列。由于它只遍歷頂部/外部陣列,因此它輸出每個嵌套陣列的 toString 方法,該方法輸出我認為是對存盤陣列位置的參考的十六進制代碼。我猜是因為這兩個嵌套陣列從未用長度實體化,它們顯示的是空值(它們還不存在)。此外,諸如 int 之類的原始型別不能為 null,因此并不是說 int 為 null。
參考第二個列印行:
ArrayList 被實體化,傳遞一個 2 作為引數。我不相信設定初始容量對列印陳述句有任何影響。ArrayList 中不包含任何嵌套的 ArrayList,因此列印其內容表明它是空的:[]
uj5u.com熱心網友回復:
根據我的說法,一旦你默認宣告了一個大小為大小的陣列,jvm 會分配記憶體并為其分配一個空值,即使你沒有在 java 中默認為它分配任何值,它也會存盤空值
但是在 arraylist 的情況下,它不會分配記憶體,因為您不需要提及任何大小
ArrayList<ArrayList<Integer>> arrList = new ArrayList<ArrayList<Integer>>(2);
這里的 2 并不意味著你被分配了 2 的大小 ArrayList 是動態的你不需要提及 ArrayList的大小
如果您需要創建 2d ArrayList 供您參考,請使用此站點 2d ArrayList
uj5u.com熱心網友回復:
ArrayList 不需要任何初始值,簡單 List 需要初始值
uj5u.com熱心網友回復:
因為呼叫的函式的實作對toString()他們來說是不同的。
這是toString()ArrayList的實作
/**
* Returns a string representation of this collection. The string
* representation consists of a list of the collection's elements in the
* order they are returned by its iterator, enclosed in square brackets
* ({@code "[]"}). Adjacent elements are separated by the characters
* {@code ", "} (comma and space). Elements are converted to strings as
* by {@link String#valueOf(Object)}.
*
* @return a string representation of this collection
*/
public String toString() {
Iterator<E> it = iterator();
if (! it.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
}
請注意,當沒有下一個元素時,"[]"將回傳此 String。
這是toString()陣列的實作
/**
* Returns a string representation of the contents of the specified array.
* If the array contains other arrays as elements, they are converted to
* strings by the {@link Object#toString} method inherited from
* {@code Object}, which describes their <i>identities</i> rather than
* their contents.
*
* <p>The value returned by this method is equal to the value that would
* be returned by {@code Arrays.asList(a).toString()}, unless {@code a}
* is {@code null}, in which case {@code "null"} is returned.
*
* @param a the array whose string representation to return
* @return a string representation of {@code a}
* @see #deepToString(Object[])
* @since 1.5
*/
public static String toString(Object[] a) {
if (a == null)
return "null";
int iMax = a.length - 1;
if (iMax == -1)
return "[]";
StringBuilder b = new StringBuilder();
b.append('[');
for (int i = 0; ; i ) {
b.append(String.valueOf(a[i]));
if (i == iMax)
return b.append(']').toString();
b.append(", ");
}
}
請注意,當陣列為空時,它為空,那么該行將b.append(String.valueOf(a[i]));將此字串附加"null"到"["
您可以在 IDE 中查看源代碼并對其進行除錯以了解更多資訊
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/514924.html
標籤:爪哇数组数组列表
上一篇:如何映射陣列未命名的物件陣列
