博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Effective Java 25 Prefer lists to arrays
阅读量:5110 次
发布时间:2019-06-13

本文共 2953 字,大约阅读时间需要 9 分钟。

Difference

Arrays

Lists

1

Covariant

Invariant

2

Reified at runtime

Erased at run time

3

Runtime type safety

Compile time type safety

   

non-reifiable types

E, List<E> and List<String>

whose runtime representation contains less information than its compile-time representation.

Reifiable types

List<?> and Map<?,?>

Reverse to above.

   

This code fragment is legal:

// Fails at runtime!

Object[] objectArray = new Long[1];

objectArray[0] = "I don't fit in"; // Throws ArrayStoreException

   

but this one is not:

// Won't compile!

List<Object> ol = new ArrayList<Long>(); // Incompatible types

ol.add("I don't fit in");

   

Prohibition on generic array creation

  1. Not possible for a generic type to return an array of its element type.
  2. Not possible for using varargs methods in combination with generic types.( This is because every time you invoke a varargs method, an array is created to hold the varargs parameters. Use warning suppress to deal with it if it's required.).

       

None of these array creation expressions are legal:

new List<E>[],

new List<String>[] ,

new E[].

All will result in generic array creation errors at compile time.

   

// Why generic array creation is illegal - won't compile!

List<String>[] stringLists = new List<String>[1]; // (1)

List<Integer> intList = Arrays.asList(42); // (2)

Object[] objects = stringLists; // (3)

objects[0] = intList; // (4)

String s = stringLists[0].get(0); // (5)

   

Let's pretend that line 1, which creates a generic array, is legal. Line 2 creates and initializes a List<Integer>containing a single element. Line 3 stores the List<String>array into an Object array variable, which is legal because arrays are covariant. Line 4 stores the List<Integer>into the sole element of the Object array, which succeeds because generics are implemented by erasure: the runtime type of a List<Integer>instance is simply List, and the runtime type of aList<String>[]instance is List[], so this assignment doesn't generate an ArrayStoreException. Now we're in trouble. We've stored a List<Integer> instance into an array that is declared to hold only List<String>instances. In line 5, we retrieve the sole element from the sole list in this array. The compiler automatically casts the retrieved element to String, but it's an Integer, so we get a ClassCastExceptionat runtime. In order to prevent this from happening, line 1 (which creates a generic array) generates a compile-time error.

   

   

Use the Lists instead of the array to check the type safety at compile time

// List-based generic reduction

static <E> E reduce(List<E> list, Function<E> f, E initVal) {

List<E> snapshot;

synchronized(list) {

snapshot = new ArrayList<E>(list);

}

E result = initVal;

for (E e : snapshot)

result = f.apply(result, e);

return result;

}

   

Summary

If you find yourself mixing them and getting compile-time errors or warnings, your first impulse should be to replace the arrays with lists.

转载于:https://www.cnblogs.com/haokaibo/p/prefer-lists-to-arrays.html

你可能感兴趣的文章
设计模式-代理模式学习笔记
查看>>
final关键字
查看>>
C++ vector,list,deque区别(转)
查看>>
冲刺第十四天个人博客
查看>>
让用户打开你app的位置功能
查看>>
Leetcode 116 Populating Next Right Pointers in Each Node
查看>>
《STL源代码剖析》---stl_deque.h阅读笔记(2)
查看>>
Python 字符串的encode与decode
查看>>
springMVC在JSP页面取map值
查看>>
智力逻辑题
查看>>
Phpcms V9导航循环下拉菜单的调用技巧
查看>>
SpringBoot前后端分离Instant时间戳自定义解析
查看>>
开发一个简单的 Vue 弹窗组件
查看>>
1076 Wifi密码 (15 分)
查看>>
面向对象抽象类及封装
查看>>
rsync
查看>>
java中的IO操作总结
查看>>
noip模拟赛 党
查看>>
bzoj2038 [2009国家集训队]小Z的袜子(hose)
查看>>
Java反射机制及其Class类浅析
查看>>