When to use Object[] and List<Object> [duplicate]
Possible Duplicate:
When to use a List over an Array in Java?
As the title states, I am trying to figure out when to use certain types of lists. I just realized that I have no idea how these Object[]-lists are implemented, and thus when they are preferred over List. Perhaps they are only preferred when I know that the size of the list w开发者_如何学Pythonont change? I hope I have made my question clear.
Cheers,
Effective Java, chapter 5:
Item 25: Prefer lists to arrays
Read the explanation of Item 25.
Also, Object[]
has fixed size, and the List
may change in size (the efficiency depends on the implementation - if it's LinkedList
, ArrayList
, etc.)
You should almost always prefer Lists (or other Collections).
Arrays are inflexible, fixed-implementation building blocks that can be used to implement Collections. But in normal application code it's rarely beneficial to use them directly.
With a List, for example, you can easily add and remove objects at any position, swap the implementation to suite your needs or even replace a List
with a SortedSet
if your requirements change.
One big exception is buffers or other arrays of primitive data: For reading from a InputStream
you will use a byte[]
and definitely not a List<Byte>
.
Object[]
is called an array - a very simple, fundamental data structure found in most programming languages. Arrays get special treatment (such as their own syntax) in both the Java language specification and the VM specification. What you need to know:
- Arrays have a fixed size
- They are slightly faster and more memory-efficient than Lists (this is unlikely to matter in most programs)
- You can have arrays of primitive types such as
int
, but not Lists - it may look like it because of autoboxing but it's really aList<Integer>
, which has a somewhat more significant time and memory overhead - You can instantiate multi-dimensional arrays in a single statement, e.g.
int[][] matrix = new int[100][100]
- much more convenient than aList<List<Integer>>
because there, you also have to create the nested lists manually.
Object[]
is not a list, it's an array. You can't resize an array, it has a fixed size upon creation. Whether you can resize a List
depends on the concrete implementation, since List
is just an interface. For example, ArrayList
and LinkedList
may have elements added and removed from them, while some lists returned by Collections
are fixed-size.
You already gave the answer yourself: if you don't expect changes to the size, use an array. If you have more dynamic behavior, use a List.
Arrays are directly arrays in memory. Lists can be built of diverse manners (with an array in the case of ArrayList for example).
As to when to use them, Lists are more flexible and the Collection framework provides really useful operations on them.
I advise you to read the chapter of Essential Java about that subject, it will enter much more in the details than me.
Object[]
isn't a list, it's an array.
Arrays in Java are allocated in contiguous blocks of memory and so indexing into them (myArray[3]
) is very efficient. They're storage-efficient too (not a lot of overhead).
Lists are different things, and in fact there is no implementation of List<Object>
; that's an interface, an abstract way of dealing with a list. There's LinkedList<Object>
and ArrayList<Object>
, etc., which are actual implementations of the interface with different trade-offs between various operations -- indexing into the list, traversing it, modifying it, etc. -- and between how much storage overhead they introduce.
Arrays are great when you're not going to change how many elements are in the array much at all; lists are great when you'll need to change how many elements there are.
精彩评论