`
leonzhx
  • 浏览: 793339 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 25: Prefer lists to arrays

阅读更多

1.  Arrays are covariant which means simply that if Sub is a subtype of Super, then the array type Sub[] is a subtype of Super[]. Generics, by contrast, are invariant: for any two distinct types Type1 and Type2, List<Type1> is neither a subtype nor a supertype of List<Type2>.

 

2.  You can’t put a String into a Long container, but with an array you find out that you’ve made a mistake at runtime; with a list, you find out at compile time.

 

3.  Arrays are reified which means that arrays know and enforce their element types at runtime. If you try to store a String into an array of Long, you’ll get an ArrayStoreException. Generics, by contrast, are implemented by erasure. This means that they enforce their type constraints only at compile time and discard (or erase) their element type information at runtime.

 

4.  It is illegal to create an array of a generic type, a parameterized type, or a type parameter: new List<E>[], new List<String>[], new E[]. If they were legal, casts generated by the compiler in an otherwise correct program could fail at runtime with a ClassCastException. For example, due to erasure, it’s allowed to put an instance of List<String> to an array of List<Integer> at runtime.

 

5.  Types such as E, List<E>, and List<String> are technically known as nonreifiable types. Intuitively speaking, a non-reifiable type is one whose runtime representation contains less information than its compile-time representation. The only parameterized types that are reifiable are unbounded wildcard types such as List<?> and Map<?,?>. It is legal to create arrays of unbounded wildcard types.

 

6.  Every time you invoke a varargs method, an array is created to hold the varargs parameters. If the element type of this array is not reifiable, you get a warning. There is little you can do about these warnings other than to suppress them.

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 28: Prefer lists to arrays Item 29: Favor generic types Item 30: Favor generic methods Item 31: Use bounded wildcards to increase API flexibility Item 32: Combine generics and varargs judiciously...

    专享:prefer的用法__(全).pdf

    13. “I would prefer to spend the weekend at home rather than driving all the way to your mother’s.”说话者宁愿在家度过周末,而不愿长途驾车去对方母亲家。 14. “I should prefer beef rather than ...

    Effective C#

    ### Effective C#: Key Insights and Best Practices ...**Item 25: Implement the Event Pattern for Notifications** - **Pattern:** Events allow objects to notify other objects of changes in...

    effective c++

    4. **Item 28: Prefer Standard Library containers to C arrays** - **核心观点**:推荐使用标准库容器(如`std::vector`、`std::list`等)代替C风格数组,以获得更好的安全性和灵活性。 - **优点分析**:标准库...

    Copy Constructors and Assignment Operators终极解释

    在C++编程语言中,复制构造函数(Copy Constructor)和赋值运算符(Assignment Operator)是两个非常关键的概念,特别是在处理对象的拷贝和赋值时。它们默认由编译器提供,但通常需要根据具体需求进行自定义,以确保正确...

    新人教九年级英语unit单词讲解PPT课件.pptx

    4. **特定情况**:`prefer to do sth.` 用于强调在特定情境下,更愿意做某事,例如:"I prefer to walk."(我宁愿走路。) 5. **对比形式**:`prefer doing sth. to doing sth.` 这种结构表示在两个动作之间,更...

    介词to的用法归纳.pdf

    * prefer to * compare to * contrast to 这些例句中,to表示比较或对比的意思,强调两者之间的差异或相似性。 五、tҏ 与 及 个 别 的 名 词 构 成 比 较 之 意 在这个用法中,to与及个别的名词构成比较之意。...

    安徽省太和县北城中心学校九年级英语全册Unit9IlikemusicthatIcandancetoSectionA1a_2d导学

    3. prefer sth to sth,如:I prefer fish to meat. 或 I prefer reading to playing. 通过以上内容的学习,学生应能理解并运用这些词汇和语法结构,进行关于音乐喜好的交流,同时也能理解和使用定语从句,提升英语...

    九年级英语下册 Unit 6 Topic1 ABCD学案(无答案) 仁爱版

    - 宁愿…也不…:prefer…to… - 儿童节目:children's program - 新闻报道:news report - 体育节目:sports program - 教育节目:educational program - 娱乐节目:entertainment program - 爱上…:fall ...

    ESLint 规则文件

    ESLint是针对javascript代码的静态检查工具。资源是本人配置的ESLint 规则文件。官方文档:http://eslint.org/docs/rules/。

    高考英语重点词组固定搭配大全.docx

    to do sth.:宁愿某人做某事,如:I prefer you to come earlier. 28. request sb. to do sth.:要求某人做某事,如:The client requested us to revise the design. 29. remind sb. to do sth.:提醒某人做某事,...

    初中要用到的固定搭配.docx

    - prefer to do sth / preferring doing sth.: 更喜欢做某事,如:I prefer to walk / I prefer walking. - begin to do sth / beginning doing sth.: 开始做某事,如:She began to cook / She began cooking. -...

    schemalint:Lint数据库架构

    public.actor.first_name: error prefer-text-to-varchar : Prefer text to varchar types 用法 安装方式: $ npm i -g schemalint 要运行,请确保您位于具有.schemalintrc.js配置文件的文件夹中,然后键入: $ ...

    2013年九年级英语下册 Module 8 On the town动词不定式专项练习 外研版

    解析:"prefer to do... rather than do..." 表示宁愿做...而不愿做...,所以两个位置都应使用动词不定式。 4. — What do you think of tomorrow’s football match? — It’s difficult for us to win the match....

    英语词汇固定搭配.doc

    - 宁愿某人做某事,例如:I prefer you to come early rather than late. 28. request sb. to do sth. - 要求某人做某事,例如:The client requested a meeting to discuss the project. 29. remind sb. to do sth...

    Google C++ Style Guide_英文版.pdf

    - **Braced Initializer List Format:** Use braced initializer lists for initializing arrays and aggregates. - **Conditionals:** Use braces for conditionals even if the body consists of a single ...

    九年级英语上册重点短语默写卷精选.doc

    8. 更愿意做…而不愿意做…:prefer to do…rather than do… 9. 从现在起:from now on 10. 使某人做某事:make sb. do sth. 11. 几个:several 12. 打开、关闭:open / close 13. 由…制成(看得见原料):be made ...

    2013年中考英语 单选题专项练习(第1集)

    7. 题目考察固定搭配:prefer to do sth. rather than do sth. 意为“宁愿做…而不愿做…”,所以B. to stay 是正确答案。 8. 题目考察动词短语的搭配:borrow 意为“借入”,lend 意为“借出”,所以A. borrow, ...

    2015秋九年级英语上册Unit5ArtworldWelcometotheunit导学案无答案新版牛津版

    - 用法包括:prefer A to B(比起B更喜欢A),prefer doing A to doing B,以及prefer that sb. (should) do sth.。 - 造句示例:I prefer reading books to watching TV. 6. **课堂活动**: - 【演练展示】部分...

Global site tag (gtag.js) - Google Analytics