Thinking in Java——笔记(16)
Arrays
Why arrays are special
- There are three issues that distinguish arrays from other types of containers: efficiency, type, and the ability to hold primitives.
- The cost of this speed is that the size of an array object is fixed and cannot be changed for the lifetime of that array.
- You should generally prefer an ArrayList to an array.
- You’ll get a RuntimeException if you exceed the bounds, indicating a programmer error.
- Arrays are superior to pre-generic containers because you create an array to hold a specific type.
Arrays are first-class objects
- The array identifier is actually a reference to a true object that’s created on the heap. This is the object that holds the references to the other objects.
- length member that tells you how many elements can be stored in that array object.
Returning an array
- Returning an array is just like returning any other object—it’s a reference.
- The garbage collector takes care of cleaning up the array when you’re done with it, and the array will persist for as long as you need it.
Multidimensional arrays
- Each vector in the arrays that make up the matrix can be of any length.
- The Arrays.deepToString( ) method works with both primitive arrays and object arrays.
Arrays and generics
- You cannot instantiate arrays of parameterized types.
- Erasure removes the parameter type information, and arrays must know the exact type that they hold, in order to enforce type safety.
- You can parameterize the type of the array itself.
- The compiler won’t let you instantiate an array of a generic type. However, it will let you create a reference to such an array.
- Although you cannot create an actual array object that holds generics, you can create an array of the non-generified type and cast it.
- A generic container will virtually always be a better choice than an array of generics.
Creating test data
Arrays.fill()
- Since you can only call Arrays.fill( ) with a single data value, the results are not especially useful.
Data Generators
- If a tool uses a Generator, you can produce any kind of data via your choice of Generator.
Array Utilities
- There are six basic methods in Arrays: equals(), fill(), binarySearch(), sort(), toString(), hashCode().
Copying an array
- The Java standard library provides a static method, System.arraycopy( ), which can copy arrays.
- If you copy arrays of objects, then only the references get copied—there’s no duplication of the objects themselves.
Comparing arrays
- To be equal, the arrays must have the same number of elements, and each element must be equivalent to each corresponding element in the other array, using the equals( ) for each element.
Array element comparisons
- You hand a Strategy object to the code that’s always the same, which uses the Strategy to fulfill its algorithm.
- sort( ) casts its argument to Comparable.
Sorting an array
- The sorting algorithm that’s used in the Java standard library is designed to be optimal for the particular type you’re sorting—a Quicksort for primitives, and a stable merge sort for objects.
Searching a sorted array
- If you try to use binarySearchC ) on an unsorted array the results will be unpredictable.
- Otherwise, it produces a negative value representing the place that the element should be inserted if you are maintaining the sorted array by hand.
- If an array contains duplicate elements, there is no guarantee which of those duplicates will be found.
Thinking in Java——笔记(16)的更多相关文章
- Java笔记16:多线程共享数据
一.Thread实现 public class ThreadDemo4 { publicstaticvoid main(String[] args) { new ThreadTest4().start ...
- java笔记整理
Java 笔记整理 包含内容 Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...
- Effective Java笔记一 创建和销毁对象
Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...
- Java笔记3-for,switch循环,格式化输出,随机数
大纲:一.分支结构 if switch二.循环 for while do while break continue三.格式化输出 [printf] int score = 100; String na ...
- java笔记00-目录
--2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:
- Ext.Net学习笔记16:Ext.Net GridPanel 折叠/展开行
Ext.Net学习笔记16:Ext.Net GridPanel 折叠/展开行 Ext.Net GridPanel的行支持折叠/展开功能,这个功能个人觉得还说很有用处的,尤其是数据中包含图片等内容的时候 ...
- SQL反模式学习笔记16 使用随机数排序
目标:随机排序,使用高效的SQL语句查询获取随机数据样本. 反模式:使用RAND()随机函数 SELECT * FROM Employees AS e ORDER BY RAND() Limit 1 ...
- golang学习笔记16 beego orm 数据库操作
golang学习笔记16 beego orm 数据库操作 beego ORM 是一个强大的 Go 语言 ORM 框架.她的灵感主要来自 Django ORM 和 SQLAlchemy. 目前该框架仍处 ...
- 转 Java笔记:Java内存模型
Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...
随机推荐
- ucenter用户登录过程
以用户登录为例介绍,其它注销,改密码,消息,头像,好友均类同. 从用户xxx在某一应用程序的login.php,输入用户名,密码讲起.先用uc_user_login函数到uc_server验证此用户和 ...
- VisualStudio控制台输出窗口一闪而过
我用到开发环境是visual studio 2010,写的简单的控制台程序,结果一按control+F5输出窗口出现后立刻就消失了(平时很少这样的呀),按照以往经验,加上了在程序结尾getchar() ...
- 【XLL 框架库函数】 TempActiveCell/TempActiveCell12
这两个函数创建 XLOPER/XLOPER12 ,包含了当前激活工作表上的单元格引用. LPXLOPER TempActiveCell(WORD row, BYTE col); LPXLOPER12 ...
- AFNetworking3.0介绍,收藏
参考:http://www.jianshu.com/p/5969bbb4af9f 很多时候,AFNetworking都是目前iOS开发者网络库中的不二选择.Github上2W+的star数足见其流行程 ...
- 用JMeter进行Performance Test
用JMeter可以对Http请求进行Performance Test,来分析哪些方法花费的时间多,哪些方法花费的时间少,有了这样的分析结果后,我们就可以集中力量来改进费时的方法. 官方文档请参考如下链 ...
- html学习第一天笔记——第七章节
第7章 CSS样式基本知识<span style="color:blue"> </span>嵌入式css样式,写在当前的文件中[**********]嵌入式 ...
- Codeforces Round #365 (Div. 2)
A题 Mishka and Game 水..随便统计一下就A了 #include <cstdio> #include <map> #include <set> #i ...
- 《DSP using MATLAB》示例Example5.15
代码: x1 = [1,2,2]; x2 = [1,2,3,4]; y1 = circonvt(x1,x2,5); % N = 5 n1 = 0:1:length(x1)-1; n2 = 0:1:le ...
- jq switch case
switch (cnt) { case ("string1"): ... ...
- 首师大附中互测题:LJX的校园:入学典礼【C003】
[C003]LJX的校园:入学典礼[难度C]—————————————————————————————————————————————————————————————————————————————— ...