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的内存模型,这里对其中的一些基本概念做个简单的笔记 ...
随机推荐
- Python的由来
Python的由来 Python这门语言是由C开发而来. C语言: 代码编译得到 机器码 ,机器码在处理器上直接执行,每一条指令控制CPU工作 其他语言: 代码编译得到 字节码 ,虚拟机执行字节码并转 ...
- WebService -- Java 实现之 CXF (初体验)
1. 认识WebService 简而言之,她就是:一种跨编程语言以及操作系统的远程调用技术. 大家都可以根据定义好的规范和接口进行开发,尽管各自的使用的开发语言和操作系统有所不同,但是由于都遵循统一的 ...
- Unity3D LuaComponent(基于ulua)
LuaComponent可以支持配一个需要执行在这个gameObject上的lua脚本,并且每个gameObject上的lua都是一个实例 using UnityEngine; using LuaIn ...
- js中数组连接concat()
数组连接concat() concat() 方法用于连接两个或多个数组.此方法返回一个新数组,不改变原来的数组. 语法 arrayObject.concat(array1,array2,...,arr ...
- webservice发布服务:AXIS2及客户端调用
1.Axis2: 到官网下载axis2的压缩包. 解压后: 1.将lib文件下的jar包复制到项目中 2.在web-inf下创建services->META-INF->services.x ...
- 数据库Blob数据类型转String
blob数据是存储大对象数据类型, 一般存放二进制的,所以才用字节存取. 首先判断blob数据是否为空,然后采用输入流读出数据,具体代码如下: String content = null; try { ...
- IE 中单元格的 colspan 属性在某些情况下会影响 TABLE 元素的自动布局
今天在写一个jsp页面时,遇到一个如下的问题:在一个table中写了如下内容,table中定义了4列,在firefox中能正常显示,而在ie8中,显示不正常, 如下如图1:第二,三,四列宽度发生变化, ...
- Revit 自定义RibbonPanel到Revit中
项目下找到TheApplication.cs,更改内容: class TheApplication : IExternalApplication { public Result O ...
- NYOJ题目27水池数目
--------------------------------------------- 这道题有点坑,也怪我总是有点马虎,按照正常人的思维0是表示有水池啊竟然是1表示有水池,最坑的是写反了竟然还能 ...
- 联合体(union)的使用方法及其本质
转自:http://blog.csdn.net/huqinwei987/article/details/23597091 有些基础知识快淡忘了,所以有必要复习一遍,在不借助课本死知识的前提下做些推理判 ...