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)的更多相关文章

  1. Java笔记16:多线程共享数据

    一.Thread实现 public class ThreadDemo4 { publicstaticvoid main(String[] args) { new ThreadTest4().start ...

  2. java笔记整理

    Java 笔记整理 包含内容     Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...

  3. Effective Java笔记一 创建和销毁对象

    Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...

  4. Java笔记3-for,switch循环,格式化输出,随机数

    大纲:一.分支结构 if switch二.循环 for while do while break continue三.格式化输出 [printf] int score = 100; String na ...

  5. java笔记00-目录

    --2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:

  6. Ext.Net学习笔记16:Ext.Net GridPanel 折叠/展开行

    Ext.Net学习笔记16:Ext.Net GridPanel 折叠/展开行 Ext.Net GridPanel的行支持折叠/展开功能,这个功能个人觉得还说很有用处的,尤其是数据中包含图片等内容的时候 ...

  7. SQL反模式学习笔记16 使用随机数排序

    目标:随机排序,使用高效的SQL语句查询获取随机数据样本. 反模式:使用RAND()随机函数 SELECT * FROM Employees AS e ORDER BY RAND() Limit 1 ...

  8. golang学习笔记16 beego orm 数据库操作

    golang学习笔记16 beego orm 数据库操作 beego ORM 是一个强大的 Go 语言 ORM 框架.她的灵感主要来自 Django ORM 和 SQLAlchemy. 目前该框架仍处 ...

  9. 转 Java笔记:Java内存模型

    Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...

随机推荐

  1. nginx自动检测后台服务器健康状态

    转自http://www.iyunv.com/thread-38535-1-1.html 公司业务线上对后端节点的健康检查是通过nginx_upstream_check_module模块做的,这里我将 ...

  2. 批量删除 svn文件

    windows下批量删除SVN的方法 ============================ 在.svn的同级目录,也就是项目的根目录 新建文件 killsvn.bat 输入内容: @echo on ...

  3. 十二. 一步步破解JEB 2.0demo版二

    编写脚本批量还愿JEB 加密字符串 解密完后效果如下: 脚本源码: https://github.com/bingghost/JebPlugins 思路: 下面的该封装的基本都封装了,过程如下: 1. ...

  4. python 小试题

    有个同事要帮一个朋友做一个小试题,题目如图: 由于个人在学习python路上,所以想用python 写出来这道题,来练练手,苦思冥想,再加上受同事的一些启发,加以扩展,写出代码如下: #!/usr/b ...

  5. Apache curator-client详解

    Apache curator框架中curator-client组件可以作为zookeeper client来使用,它提供了zk实例创建/重连机制等,简单便捷.不过直接使用curator-client并 ...

  6. sql server 中不同服务器上的数据库中表怎么互导数据

    --创建一个链接 EXEC sp_addlinkedserver 'OtherServerDb','','SQLOLEDB','192.168.12.101' --第一个参数是给远程的服务器起的别名 ...

  7. 15. 3Sum_左右开工,遍历找出符合目标的数字

    题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

  8. C#运算符

    运算符 1.算数运算符 赋值运算符 等号在C#中并不是表示相等的意思,而是表示赋值,把等号右边的值赋值给 等号左边的变量 由等号连接的表达式,叫做赋值表达式.我们要求等号两边的数据类型必须一 致. 2 ...

  9. Linux虚拟机添加新硬盘的全程图解

    查看网的文章,我将在vm虚拟机LinuxRedhat中添加一个新的硬盘, 过程大致如下: 1.选择"VM"----"setting"并打开,将光标定位在hard ...

  10. 环境变量/path/classpath/JAVA_HOME/JAVA环境变量配置

    环境变量 环境变量是在操作系统中一个具有特定名字的对象,它包含了一个或者多个应用程序所将使用到的信息.例如Windows和DOS操作系统中的path环境变量,当要求系统运行一个程序而没有告诉它程序所在 ...