Use of array of zero length

后端 未结 3 1235
梦毁少年i
梦毁少年i 2020-11-28 10:19

For example we can construct such an array like this:

 new ElementType[0];

I seen such a construct, but I don\'t understand why this might

3条回答
  •  被撕碎了的回忆
    2020-11-28 10:56

    it is a replacement for null since you don't need to check for null before use it. More formally it is a special case of special case design pattern (check also Null Object).

    Another idiomatic use is collections toArray:

    List list = new ... ;
    // fill the list
    String[] array = list.toArray(new String[0]); 
    

提交回复
热议问题