JDK 9 List.of(...)
发布时间:2026/9/6 19:33:28 作者:尧图编辑部 阅读量:1,286
)
// JDK 9 写法: List.of(...) // ListPoint polygon List.of(new Point(0, 0), new Point(0, 4), new Point(4, 4), new Point(4, 0)); // JDK 1.6 写法使用ArrayList并手动add元素 ListPoint polygon new ArrayListPoint(); polygon.add(new Point(0, 0)); polygon.add(new Point(0, 4)); polygon.add(new Point(4, 4)); polygon.add(new Point(4, 0));JDK 9 List.of(...) JDK 1.6 boolean add(E e);/** * Appends the specified element to the end of this list (optional * operation). * * pLists that support this operation may place limitations on what * elements may be added to this list. In particular, some * lists will refuse to add null elements, and others will impose * restrictions on the type of elements that may be added. List * classes should clearly specify in their documentation any restrictions * on what elements may be added. * * param e element to be appended to this list * return tttrue/tt (as specified by {link Collection#add}) * throws UnsupportedOperationException if the ttadd/tt operation * is not supported by this list * throws ClassCastException if the class of the specified element * prevents it from being added to this list * throws NullPointerException if the specified element is null and this * list does not permit null elements * throws IllegalArgumentException if some property of this element * prevents it from being added to this list */ boolean add(E e);没有什么为什么一方面以前做的项目1.6 1.8 要兼容另一方面代码习惯明了比简洁还要重要。