如果是一个ArrayList preferable到Java中的数组? [英] When is an ArrayList preferable to an array in Java?

查看:162
本文介绍了如果是一个ArrayList preferable到Java中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我应该使用ArrayList在Java中,当我应该使用数组?

When should I use an ArrayList in Java, and when should I use an array?

推荐答案

有些差异:


  • 数组是在规模不变的,你不能删除伊斯利和元素,并删除孔,而使用ArrayList是简单

  • 数组是快比一个ArrayList(由JVM作为特殊对象直接处理),并且需要更少的内存

  • 阵列具有访问元素一个不错的语法(如 A [I] VS a.get(我)

  • 阵列不与仿制药(例如你不能创建一个通用的阵列)
  • 发挥出色
  • 数组不能伊斯利包装成的ArrayList(如集合 utils的如 checkedList synchronizedList unmodifiableList

  • 声明的ArrayList 列表你可以伊斯利交换与实施的LinkedList 当你需要;恕我直言,这是最好的优势超过普通阵列

  • Array的的toString 等于散code 怪异,而且容易出错,必须使用阵列类应用

  • Arrays are immutable in their size, you cannot easly remove and element and remove the hole whereas using an ArrayList is straightforward
  • Arrays are fast (handled directly by the JVM as special objects) than an ArrayList and requires less memory
  • Arrays have a nice syntax for accessing elements (e.g. a[i] vs a.get(i))
  • Arrays don't play well with generics (e.g. you cannot create a generic array)
  • Arrays cannot be easly wrapped as ArrayList (e.g. Collections utils like checkedList, synchronizedList and unmodifiableList)
  • declaring the ArrayList as List you can easly swap implementation with a LinkedList when you need; this imho is the best advantage over plain arrays
  • Array's toString, equals and hashCode are weird and error-prone, you must use Arrays class utilities

这篇关于如果是一个ArrayList preferable到Java中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆