数组是一种效率最高的存储和随机访问对象引用序列的方式,数组有固定的尺寸,是同一数据类型的集合。
数组相比其他容器具有效率、类型和保存基本类型的优势,缺点是数组只是个存储机制,没有丰富的方法可用,也没有自动扩容的机制。
1.1.
基本类型与性能优势
数组可以持有基本类型,而容器则不能,容器看起来能够持有基本数据类型,其实是通过自动装箱拆箱机制,把基本类型转为对应的包装类型对象。自动装箱拆箱是需要开销的,而且对象占用的内存更多。
1.2.
Java数组协变类型导致运行时异常
Java数组是可以协变类型的,即一个
String
数组也是一个
Object
数组。但不恰当的数组协变可能导致异常。
String[] strings =
new
String[10];
strings[0] =
"123"
;
Object[] objects = strings;
objects[1] = Integer.
valueOf
(10);
这几行代码在编译时没错,但在运行时会抛出这样的异常:
Exception in thread "main"
java.lang.ArrayStoreException
: java.lang.Integer
使用列表则可以避免这种错误,
List<
Object
> list =
new
LinkedList<String>()
;
这行代码就不能编译通过,因为列表的类型不是协变的。
优先使用
链表lists
而不是数组
arrays
。
1.3.
数组拷贝
数组拷贝不应该自己编写循环来完成,应该使用System.
arraycopy
方法来完成,除了代码更简洁外,还能带来性能的提升,因为这是个本地方法,而且针对每种基本类型进行重写,性能是最优的。
还可以使用java.util.Arrays
的
copy*
方法,这些方法不需要提供目标数组,在方法内新建数组,调用
System.
arraycopy
进行数据拷贝,然后返回新数组,
简化了用户调用。
1.4.
Arrays工具类
Arrays工具类提供了数组的:搜索、拷贝、部分拷贝、比较、
hashcode
计算、填充、排序、转换为字符串 的功能,对数组进行操作,应优先考虑这些方法。
public class LearnArrays {
public static void main(String[] args) {
int[] iarr = new int[] { 3, 2, 4, 5, 8, 7, 6, 1, 9, 0 };
int index = Arrays.binarySearch(iarr, 8); // 搜索
System.out.println("index of 8 is :" + index);
int[] copy = Arrays.copyOf(iarr, 4); // 拷贝
String copyString = Arrays.toString(copy); // 把数组内容表示为String
System.out.println(copyString);
Arrays.sort(iarr); // 排序
System.out.println("after sort :" + Arrays.toString(iarr));
int code = Arrays.hashCode(iarr); // 计算hashcode
System.out.println("hash code :" + code);
}
}
1.5.
总结
A、
优先使用
链表lists
,而不是数组
arrays
。
B、
如果非要持有基本数据类型的容器,可以使用Apache Commons Primitives
库。
分享到:
相关推荐
useEnglish 资源E nglish Phrasal Verb & Idiom
四字成语mysql数据30810条
iom` (`id`, `guid`, `idiom`, `full_pinyin`, `pinyin`, `az`, `description`, `source`, `idiom_traditional`, `spinyin`, `idiom_phonetic`, `idiom_correct_pronunciation`, `idiom_distinguish`, `idiom_...
好的翻译软件,能用于有效的翻译,是一个非常不错的软件。本人经过测试,完全好用。
成语Idiom 是 AngularJS 的新翻译系统,它允许您在当前范围内阅读以提供基于上下文的翻译 - 没有更多的东西,他/她,您实际上知道您在写什么。 #用法向您的项目添加一个文件夹,其中包含每种语言的一个 json 文件...
Idiom WorldServer Desktop Workbench 8.1.0.208 使用指南
This is a common I/O programming idiom. while((bytes_read = from.read(buffer)) != -1) // Read until EOF to.write(buffer, 0, bytes_read); // write } // Always close the streams, even if exceptions...
AI-Algorithm-Idiom-in-Prolog-Lisp-Java 《Prolog、Lisp 和 Java 中的 AI、Alogirhtm、数据结构和习语》一书的代码
猜成语小程序数据库表ims_yf_chengyu_idiom
实现类 接口和实现的分离 采用pimpl idiom方式
java 8 lambda Concise syntax – More succinct and clear than anonymous inner classes • Deficiencies with anonymous inner classes – Bulky, confusion re “this” and naming in general, no nonfinal var...
资源来自pypi官网。 资源全名:china_idiom-0.0.2-py3-none-any.whl
Emotional Classification of Chinese Idioms Based on Chinese Idiom Knowledge Base
自然语言处理相关的分词数据
Your first Java program ....... 52 Compiling and running ............... 54 Comments and embedded documentation ..................... 55 Comment documentation ............ 55 Syntax .....................
语言:English idiom有助于您阅读外语网站。 点击扩展,然后选择任何单词以说话和翻译。 阅读外国新闻,文学等。
英语习语idiomPPT课件.pptx
American Idioms and Some Phrases Just For Fun
Chinese language idiom is one of the most distinctive categories, which is the focus of the senior stage of the foreign students. This paper conducts a comprehensive investigation based on idiom in ...