2020/11/18·Java·Array(一维)

数组概念

同一种类型数据的集合。用来存储固定大小的同类型元素。

数组可以装任意类型的数据,但是定义好的数组只能装一种元素,也就是数组一旦定义,那么里边存储的数据类型也就确定了。

无论哪种类型,数组标识符只是一个引用,指向堆中创建的一个真实对象,这个(数组)对象用以保存指向其他对象的引用


数组声明定义

1.元素类型[] 数组名 = new 元素类型[元素个数或数组长度];

int[] arr = new int[5];

2.元素类型[] 数组名 = new 元素类型[]{元素,元素,……};

int[] arr = new int[]{3,5,1,7};
int[] arr = {3,5,1,7};

数组常用方法

循环:

int[] arr = {1, 2, 3, 4, 5, 4, 3};
//for 循环
for (int i = 0; i < arr.length; i++){
    System.out.println(arr[i]);
}
//foreach 循环
for (int a : arr){
    System.out.println(a);
}

打印数组:

int[] intArray = { 1, 2, 3, 4, 5 };
//转换为字符串
String intArrayString = Arrays.toString(intArray);
System.out.println(intArray);
// [I@7150bd4d
System.out.println(intArrayString);
// [1, 2, 3, 4, 5]

根据数组创建ArrayList

String[] stringArray = { "a", "b", "c", "d", "e" };
List<String> arrayList = new ArrayList<>(Arrays.asList(stringArray));
System.out.println(arrayList);
// [a, b, c, d, e]

判断数组是否包含某个值

String[] stringArray = { "a", "b", "c", "d", "e" };
boolean b = Arrays.asList(stringArray).contains("a");
System.out.println(b);
// true

连接两个数组

int[] intArray = { 1, 2, 3, 4, 5 };
int[] intArray2 = { 6, 7, 8, 9, 10 };
int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);

内联数组

method(new String[]{"a", "b", "c", "d", "e"});

根据分隔符拼接元素

String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");
System.out.println(j);

ArrayList 转数组

String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
String[] stringArr = new String[arrayList.size()];
arrayList.toArray(stringArr);
for (String s : stringArr)
    System.out.println(s);

Array 转 Set

Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
System.out.println(set);
//[d, e, b, c, a]

反转数组

int[] intArray = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(intArray);
System.out.println(Arrays.toString(intArray));
//[5, 4, 3, 2, 1]

删除数组元素

int[] intArray = { 1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
System.out.println(Arrays.toString(removed));

整型转字节数组

byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
for (byte t : bytes) {
    System.out.format("0x%x ", t);
}

java.util.Arrays 类能方便地操作数组,它提供的所有方法都是静态的。

序号方法和说明
1public static int binarySearch(Object[] a, Object key)
用二分查找算法在给定数组中搜索给定值的对象(Byte,Int,double等)。
数组在调用前必须排序好的。如果查找值包含在数组中,则返回搜索键的索引;
否则返回 (-(插入点) - 1)。
2public static boolean equals(long[] a, long[] a2)
如果两个指定的 long 型数组彼此相等,则返回 true。
如果两个数组包含相同数量的元素,并且两个数组中的所有相应元素对都是相等的,
则认为这两个数组是相等的。
换句话说,如果两个数组以相同顺序包含相同的元素,则两个数组是相等的。
同样的方法适用于所有的其他基本数据类型(Byte,short,Int等)。
3public static void fill(int[] a, int val)
将指定的 int 值分配给指定 int 型数组指定范围中的每个元素。
同样的方法适用于所有的其他基本数据类型(Byte,short,Int等)。
4public static void sort(Object[] a)
对指定对象数组根据其元素的自然顺序进行升序排列。
同样的方法适用于所有的其他基本数据类型(Byte,short,Int等)。

数组优缺点

优点: 元素从0开始自动编号,方便操作;数组是简单的线性序列,通过下标元素访问元素非常快速。

缺点: 数组长度固定、不容易扩容,数据类型必须要一致,这也导致在实际工作中并不实用;数组进行元素的删除和插入操作的时候,效率比较低,需要移动大量的元素;内存空间连续分配,对内存空间要求比较高。


一文一哲理

一个人真正的资本,不是美貌,也不是金钱,而是人品

最后麻烦朋友们的小手手点点赞,你们的点赞是我创作最大的动力!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值