JSE:数组

数组

基本语法

  • 所有的数组元素 具有 相同的数据类型( 基本类型的数据 / 引用类型的数据)

  • 数组 初始化 后, 长度是不可变的

  • 数组 本身 是一种 引用类型

定义数组
  • 定义的时候不能指定数组长度
int [] intArr0; 
初始化
1.静态初始化
  • 由 代码 显示 指定每个元素的初始化, 由 系统 决定 数组长度
/* 1.静态初始化: 显示指定每个数组元素的初始值  由系统决定数组长度 */
intArr0 = new int [] {5, 6, 8, 20}; 

/* 2.初始化的时候的type可以是定义的时候type的 子类 */
Object [] objArr;
objArr = new String[] {"java", "hello"}; 

/* 3. 定义 + 静态初始化  */
int [] intArr1 = new int[] {5, 6};

/* 4. 定义 + 静态初始化  - 简化语法 */
int [] intArr2 = {5, 6};

/* 5.使用var 简化代码 */
var intArr3 = new int[] {5, 6};
2.动态初始化
  • 由 代码 显示 指定数组长度, 由 系统 分配初始值

    基本类型.整数类型(byte/short/int/long)val = 0
    基本类型.浮点类型(float/double)val = 0.0
    基本类型.字符类型(char)val = `\u0000`
    基本类型.布尔类型(boolean)val = false
    引用类型(类、接口、数组)val = null
/* 1.动态初始化: 显示指定数组长度,由系统决定数组初始值 */
int[] intArr0;
intArr0 = new int[4]; 

 /* 2.定义+动态初始化 */
int[] intArr1 = new int[4];
Object[] objArr = new String [3];

 /* 3.使用var 简化代码 */
var intArr2 = new int[4];
使用
length属性
int[] prices = new int[4];
System.out.println(prices.length); /* 数组 提供了一个 length属性 */
equal
  • 基本的数据类型的数组的equals方法没有重载Object的equals方法,所以跟“==”效果一样 、

    int[] a = {1,2};  
    int[] b = a;  
    System.out.println(a.equals(b));   /* 返回true。因为b和a都指向同一个数组对象 */
    
存储
  • int [] p; p 是一个引用变量 , 可能存储在 栈内存中(如:局部变量) ,而其实际的数组对象是在 堆内存中
  • 引用类型数组中的数组元素存储的还是引用
二维数组
/* 定义一个二维数组 */
int [][] a;

/* 将a当成一维数组初始化 , 数组元素仍是引用类型 */
a = new int [4][];

/* 初始化a数组的第一个元素 : 一个一维数组 */
a[0] = new int[2];
/* 动态初始化 */
int [][] a = new int[3][4];

/* 静态初始化 */
String [][] str1 = new String [][] { new String [3], new String [] {"Hello"}};
注意事项
  • 不可以同时使用静态和动态初始化,即数组初始化时 不可以既指定数组长度 又 分配初始值

  • foreach循环 不能改变数组元素的值。 它是将数组元素依次赋给了一个临时变量

  • 数组是一个Object对象

Arrays类

  • Arrays类 在 java.util包中 import java.util.Arrays
  • from the index fromIndex, inclusive, to the index toIndex, exclusive : toIndex 是不被包含的 即 可取到 a.length
Arrays.sort
  • ascending order 升序排列
void sort(type[] a);
void sort(type[] a, int fromIndex, int toIndex) ;
Arrays.fill
  • 数组中的某段元素赋相同值
  • from the index fromIndex, inclusive, to the index toIndex, exclusive : toIndex 是不被包含的 即 可取到 a.length
void fill(type[] a, type val);
void fill(type[] a, int fromIndex, int toIndex, type val);
Arrays.toString
  • 将数组转换为字符串。 用逗号, 和 空格 隔开
Arrays.equals
  • 如果2个数组 长度相等 且 元素一一相同 则返回true
  • from the index fromIndex, inclusive, to the index toIndex, exclusive
  • 判断的是数组中元素值是否相等。
    与之有区别的是 数组的equals方法
boolean	equals(type[] a, type[] b);
boolean	equals(type[] a, int aFromIndex, int aToIndex, type[] b, int bFromIndex, int bToIndex);
Arrays.copyOf / Arrays.copyOfRange
  • 如果newLength 小于原数组长度 则截断; 如果大于,则补充 0/false/null
  • from the index fromIndex, inclusive, to the index toIndex, exclusive
<T> T[]	copyOf (T[] original, int newLength);

<T> T[] copyOfRange (T[] original, int from, int to);  
Arrays.binarySearch
  • 使用二分法查询 key 元素 在数组中出现的索引
  • 数组必须已经升序排列
  • 若不包含则返回负数
  • from the index fromIndex, inclusive, to the index toIndex, exclusive
int	binarySearch (type[] a, type key);
int	binarySearch (type[] a, int aFromIndex, int aToIndex, type key);
Arrays.asList
  • 将数组转换成集合
  • 把数组转换成集合时,不能使用其修改集合相关的方法,它的add/remove/clear方法会抛出UnsupportOperationException异常
  • 使用基本数据类型数组时,编译器会默认将比如int[],char[]等当做一个类型,认为只传了一个变量,转换成的集合size为1
int[] arri = {1, 2, 3};
List list = Arrays.asList(arri);
System.out.println(list.size());
Integer[] arrInt = {1, 2, 3};
List integerList = Arrays.asList(arrInt);
System.out.println(integerList.size());
结果:
1
3
integerList.add(new Integer("434"));
结果:Exception in thread "main" java.lang.UnsupportedOperationException

(该小节注意事项&示例转载于
版权声明:本文为CSDN博主「hughjin」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/baidu_25310663/article/details/84992176)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值