JDK源码阅读-------自学笔记(五)(浅析数组)
一、数组基础
1、定义和特点
- 数组也可以看做是对象,数组变量属于引用类型,数组中每个元素相当于该队形的成员变量,数组对象存储在堆中.
2、初始化数组
- 常用类初始化
// 整型初始化
int[] integerInitialization = new int[10]; - 对象初始化
// 对象初始化
User[] usersInitialization = new User[10];
3、数组赋值
- 动态初始化(根据数组角标)
// 整型初始化
int[] integerInitialization = new int[10]; // 整型赋值
integerInitialization[0] = 1;
integerInitialization[1] = 2; - 循环赋值
// 整型初始化
int[] integerInitialization = new int[10]; // 循环赋值
for (int i = 0; i < integerInitialization.length; i++) {
integerInitialization[i]=10*i;
} - 静态初始化
常用类初始化// 整型初始化
int[] integerInitialization = {1,2,3,4,5,6,7,8,9};对象初始化
// 对象静态初始化
User[] usersInitialization ={new User(101,"龙五"),new User(102,"李四")}; - 默认初始化
注意:当默认初始化的时候,会按照设置的数组大小自动填入数组长度多个0,布尔型为false,引用型为null.// 默认初始化
int[] integerInitialization = new int[3]; // 测试查看默认值
System.out.println("第一个元素"+integerInitialization[0]);
System.out.println("第二个元素"+integerInitialization[1]);
System.out.println("第三个元素"+integerInitialization[2]);
4、数组遍历取值
- 循环
// 整型初始化
int[] integerInitialization = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // 循环获取元素
for (int i = 0; i < integerInitialization.length; i++) {
System.out.println("第" + (i + 1) + "个元素" + integerInitialization[i]);
} - foreach语句
// 整型初始化
int[] integerInitialization = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // foreach获取元素
for (int i : integerInitialization) {
System.out.println("第" + i + "个元素" + i);
}
二、数组拷贝
- 容器拷贝,底层就是数组拷贝
- 使用方法
源码分析:/**
* @param src 源数组(从这个数组中拷出元素).
* @param srcPos 源数组起始位置.
* @param dest 目的数组(拷贝到这里元素).
* @param destPos 目的数组起始位置.
* @param length 拷贝数组的长度(拷贝多少个元素到目的数组).
* @throws IndexOutOfBoundsException 拷贝会产生数组越界的异常.
* @throws ArrayStoreException 源数组和目的数组类型要一致,否则,会产生类型不一致的异常.
* @throws NullPointerException 源数组和目的数组中有一个为空,就会产生空指针异常.
*/
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);使用实例:
// 整型初始化
int[] integerInitialization = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // 目的数组默认初始化
int[] destPosition = new int[10]; // 数组拷贝,从integerInitialization的第一个位置起拷贝8个元素到目的数组第二个位置起
System.arraycopy(integerInitialization, 0, destPosition, 1, 8); System.out.println("数组中得到的拷贝元素:" + Arrays.toString(destPosition)); - 数组删除的本质,也是拷贝实现的方式
class DemoApplicationTests { public static void main(String[] args) { // 整型初始化
int[] integerInitialization = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // 删除第五个元素,就是6
integerInitialization = deletedElements(integerInitialization, 5, 5, integerInitialization.length); // 删除后的结果
System.out.println("数组中得到的拷贝元素:" + Arrays.toString(integerInitialization)); } /**
* 数组删除元素的本质,是数组自身的拷贝
* <p>
* 算法:
* 数组从startCopyLocation个位置开始拷贝,startCopyLocation后的元素被拷贝了,然后向前移动了一位重新放回数组
*
* @param objects 需要修改的数组
* @param startCopyLocation 拷贝元素的起始位置
* @param deletedLocation 存放元素的起始位置
* @param length 拷贝几个元素
* @return 删除后的数组
*/
private static int[] deletedElements(int[] objects, int startCopyLocation, int deletedLocation, int length) { int[] newList = objects; System.arraycopy(newList, startCopyLocation + 1, newList, deletedLocation, length - deletedLocation - 1); newList[length - 1] = 0; return newList; } }
三、数组扩容
- 算法:先定义一个更大的数组,然后将原来数组的内容原封不动的拷贝到新数组中
class DemoApplicationTests { public static void main(String[] args) { // 整型初始化
int[] integerInitialization = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // 在2的位置添加一个元素10
integerInitialization = addElement(integerInitialization, 2, 10); // 删除后的结果
System.out.println("数组中添加元素:" + Arrays.toString(integerInitialization)); } /**
* 插入元素
*
* @param ints 需要插入元素的数组
* @param elementLocation 插入元素的位置
* @param value 插入的值
* @return 新的数组
*/
private static int[] addElement(int[] ints, int elementLocation, int value) { // 数组扩容
int[] newElements = new int[ints.length << 1]; // 拷贝elementLocation个元素
System.arraycopy(ints, 0, newElements, 0, elementLocation + 1); // 添加要插入的元素
newElements[elementLocation + 1] = value; // 在插入的元素后,把数组原来后边的元素拷贝进来
System.arraycopy(ints, elementLocation + 1, newElements, elementLocation + 2, ints.length - elementLocation - 1); return newElements;
} }
JDK源码阅读-------自学笔记(五)(浅析数组)的更多相关文章
- JDK源码阅读-------自学笔记(一)(java.lang.Object重写toString源码)
一.前景提要 Object类中定义有public String toString()方法,其返回值是 String 类型. 二.默认返回组成 类名+@+16进制的hashcode,当使用打印方法打印的 ...
- JDK源码阅读-------自学笔记(二十五)(java.util.Vector 自定义讲解)
Vector 向量 Vector简述 1).Vector底层是用数组实现的List 2).虽然线程安全,但是效率低,所以并不是安全就是好的 3).底层大量方法添加synchronized同步标记,sy ...
- JDK源码阅读-------自学笔记(二十四)(java.util.LinkedList 再探 自定义讲解)
一.实现get方法 1.一般思维实现思路 1).将对象的值放入一个中间变量中. 2).遍历索引值,将中间量的下一个元素赋值给中间量. 3).返回中间量中的元素值. 4).示意图 get(2),传入角标 ...
- JDK源码阅读(三):ArraryList源码解析
今天来看一下ArrayList的源码 目录 介绍 继承结构 属性 构造方法 add方法 remove方法 修改方法 获取元素 size()方法 isEmpty方法 clear方法 循环数组 1.介绍 ...
- JDK源码阅读(一):Object源码分析
最近经过某大佬的建议准备阅读一下JDK的源码来提升一下自己 所以开始写JDK源码分析的文章 阅读JDK版本为1.8 目录 Object结构图 构造器 equals 方法 getClass 方法 has ...
- 利用IDEA搭建JDK源码阅读环境
利用IDEA搭建JDK源码阅读环境 首先新建一个java基础项目 基础目录 source 源码 test 测试源码和入口 准备JDK源码 下图框起来的路径就是jdk的储存位置 打开jdk目录,找到sr ...
- JDK源码阅读-FileOutputStream
本文转载自JDK源码阅读-FileOutputStream 导语 FileOutputStream用户打开文件并获取输出流. 打开文件 public FileOutputStream(File fil ...
- JDK源码阅读-FileInputStream
本文转载自JDK源码阅读-FileInputStream 导语 FileIntputStream用于打开一个文件并获取输入流. 打开文件 我们来看看FileIntputStream打开文件时,做了什么 ...
- JDK源码阅读-ByteBuffer
本文转载自JDK源码阅读-ByteBuffer 导语 Buffer是Java NIO中对于缓冲区的封装.在Java BIO中,所有的读写API,都是直接使用byte数组作为缓冲区的,简单直接.但是在J ...
随机推荐
- 虚拟机安装安全狗apache服务的一些问题解决方式(11.5)
首先本文鸣谢bonga的解答大部分问题=.= 由于本人比较懒所以还是喜欢问,不喜欢查啦 1.windows网站安全狗分为:IIS 和 APACHE 版本 我下载的是APACHE版本 (因为 ...
- 第3节 sqoop:7、通过java代码远程连接linux执行shell命令
数据库的数据同步软件sqoop 数据同步 关系型数据库到大数据平台 任务:sqoop 是批量导入数据太慢,如何做到实时的数据同步 实时的数据同步工具: canal 阿里开源的一个数据库数据实时同步的软 ...
- Docker基本使用运行ngix镜像
docker pull 项目名 会从docker默认的仓库去拉去项目,如果是docker pull 项目名 地址 会从给定地址拉去镜像 docker run image名字 运行镜像 docker架 ...
- 主机ping虚拟机失败。虚拟机ping主机,可以ping通。
原文:https://blog.csdn.net/ww1473345713/article/details/51490525 今天打开虚拟机,然后用Xshell远程连接,发现连接不上.按照以下顺序检查 ...
- CodeForces - 755B PolandBall and Game(博弈)
题意:A和B两人每人都熟悉一些单词.A先开始,每人说一个单词,单词不能与两人之前说过的所有单词重复,谁无话可说谁输.两人可能有共同会的单词. 分析:因为要让对方尽量无单词可说,所以每个人优先说的都是两 ...
- Gerrit部署成功后project下不显示clone地址
gerrit部署成功后使用admin账号登录,在project All-projects下不显示clone地址,新建仓库也不显示. 原因是:默认安装没有安装插件download-commands 安装 ...
- hibernate字段值无法保存
通过hibernate对Blogstorage对象进行保存操作,filepath属性的值无论设置多少遍都保存不进去 后来发现是 Blogstorage.hbm.xml 里面根本没有配置filepath ...
- Java Integer Addition Subtration Overflow 整数加减溢出
leetCode有道题Reverse Integer,因为int的最大值为2的31次方减一,最小值为-2的31次方. 我一开始的代码将res递归加放在try中,以为溢出会有异常,然而并没有. 因为出传 ...
- 腾讯云服务器上搭建Jenkins配置邮箱通知
1,Jenkins 点击 系统管理 2,点击系统管理 3,配置系统管理员邮件地址 5,配置 Extended E-main Notification,(用户名不需要邮箱后缀“@163.com”, SS ...
- spring boot 开发环境搭建(Eclipse)
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...