int arr[][] = new int[2][];
System.out.println(Arrays.toString(arr));
The above code snippet when run, creates a new 2D array with 2 elements, each element an array itself. But since I haven't specified the second pair of [] during array initialization, the print statement gives me [null, null].
My question is why do I get only [] when I run the below code;
ArrayList<ArrayList<Integer>> arrList = new ArrayList<ArrayList<Integer>>(2);
System.out.println(arrList);
According to me, since I created an ArrayList of initial capacity = 2, when I print the ArrayList I should've gotten [null,null], similar to what I got when I printed the above array.
I'm new to Java, so any help would be appreciated.
CodePudding user response:
I'm pretty new myself, but I will try my best to answer this.
During the first print line:
The Arrays.toString method is iterating over the outer array only. Since it is only traversing the top/outer array, it is outputting the toString method of each nested array, which outputs the hex code which I think is a reference to where the array is stored. I am guessing because the two nested arrays were never instantiated with a length, they are showing null values (they don’t exist yet). Also, primitive types such as int cannot be null, so it's not saying an int is null.
In reference to the second print line:
The ArrayList is instantiated, passing a 2 as a parameter. I don’t believe setting the initial capacity has any impact to the print statement. The ArrayList does not contain any nested ArrayLists within it, so printing its contents is showing that it is empty: []
CodePudding user response:
According to me once you declare an array with size by default jvm allocates the memory and assign a null values to it even though you didn't assign any value to it in java by default it stores null value
But in the case of arraylist it doesn't allocates memory because you don't need to mention any size
ArrayList<ArrayList<Integer>> arrList = new ArrayList<ArrayList<Integer>>(2);
here the 2 doesn't mean that you are allocated the size of 2 ArrayList is dynamic you don't need to mention the size ArrayList
for your reference if you need to create 2d ArrayList then use this site 2d ArrayList
CodePudding user response:
ArrayList doestn't need any initial value and simple List requires initial values
CodePudding user response:
because the implementation of the function called toString()
is different for both of them.
this is the implementation of the toString()
for the 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(' ');
}
}
notice that when there is no next element, this String "[]"
will be returned.
this is the implementation of the toString()
for the Arrays
/**
* 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(", ");
}
}
notice that when the array is empty then it's null, then the line b.append(String.valueOf(a[i]));
will append this string "null"
to "["
you can view the source code in your IDE and debug it for more understanding