关于一个基本语法,求解!!
java核心技术 英文版 P695:
The Collections class has methods that produce unmodifiable views of collections. These
views add a runtime check to an existing collection. If an attempt to modify the collection
is detected, then an exception is thrown and the collection remains untouched.
You obtain unmodifiable views by six methods:
Collections.unmodifiableCollection
Collections.unmodifiableList
Collections.unmodifiableSet
Collections.unmodifiableSortedSet
Collections.unmodifiableMap
Collections.unmodifiableSortedMap
Each method is defined to work on an interface. For example, Collections.unmodifiableList
works with an ArrayList, a LinkedList, or any other class that implements the List interface.
For example, suppose you want to let some part of your code look at, but not touch, the
contents of a collection. Here is what you could do:
List<String> staff = new LinkedList<String>();
. . .
lookAt(new Collections.unmodifiableList(staff));
The Collections.unmodifiableList method returns an object of a class implementing the List
interface. Its accessor methods retrieve values from the staff collection. Of course, the
lookAt method can call all methods of the List interface, not just the accessors. But all
mutator methods (such as add) have been redefined to throw an UnsupportedOperationException
instead of forwarding the call to the underlying collection.
你可能说太多了,看不过来,没关系,有疑问的就一句。
上文大概意思是说,Collections.unmodifiableList返回的是一个不可更改的、实现了list接口的view。之后调用lookAt方法,对这个list进行操作。(这个lookAt方法是一个假设的方法,代表只查看不操作的一个方法,此处也是第一次出现,不要问我具体是干什么的)
其中,有疑问的是lookAt(new Collections.unmodifiableList(staff));
这句是怎么回事?Collections.unmodifiableList(staff) 明明是一个方法,怎么能在方法前面加new呢?
我怀疑是打错了,可是查看了官方网站关于错误的列表,没有这个。
您可能会说,可能这个错误还没有被发现。我告诉您,错误列表里连多一个"and"这样的微不足道的错误都能发现,怎么肯能错过一条语法错误呢?
不信的可以看我以前的帖子,当时为了方便大家,我把java核心技术里所有错误都列出来了(源于官方网站)
给个链接:http://topic.csdn.net/u/20120202/11/057a92df-5bd5-4662-b53f-f651219cd2ac.html
现在回归正题,真心求教刚才的那句!
------解决方案--------------------我也觉得是笔误,书嘛,总有错误的地方。。这样写编译通不过
------解决方案--------------------全信书不如无书
------解决方案--------------------每本书都会有错误的
------解决方案--------------------人无完人,书无完书哦!
------解决方案--------------------应该是笔误,LZ你的理解没问题的