A newly created ArrayList
without adding any elements to it looks just like an ArrayList
with an empty string added in it.
This makes me feel that a newly created ArrayList
even without adding any elements in it has a default empty string
or element added in it.
package cracking;
import java.util.ArrayList;
public class Ask
{
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
System.out.println(al.size());//0
System.out.println(al);//[]
al.add("");
System.out.println(al.size());//1
System.out.println(al);//[]
}
}
However, the discrepancy between the size of ArrayList
in the two scenarios makes them inconsistent, especially since the two ArrayList
when printed out look just the same.
To maintain consistency, I feel, either of two would've been good:
- The size of the ArrayList which is just created, i.e., even before
adding any element to it should show 1 implying empty string or
element, since even adding to the
ArrayList
makes it look the same.
- Printing out a newly created
ArrayList
should not be allowed, it
should just print NULL or something instead of showing []
like it
shows now
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…