ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

重学JAVA-数组

2021-11-28 10:30:00  阅读:165  来源: 互联网

标签:arr 重学 JAVA int Arrays 数组 array public


初始化

数组的是时候一般有两种方式,一种是先分配内存空间再初始数据,另一种是在初始化数组的时候直接指定数据。

int[] array1 = new int[10];
array[1] = 10;

int[] array2 = new int[]{10,11};

另外,还有一种方式是通过反射进行数组的创建和初始化,这种方式能够动态的创建不同类型的数组。

int[] intArr = (int[]) Array.newInstance(int.class, 10);
Array.set(intArr, 0, 10);
System.out.println(Array.get(intArr, 0));

特性

数组本身是一种引用类型,数组的长度在创建完成后就不能够进行修改了。数组中的内容可以通过索引来访问,索引范围是0 - (length-1),在内存中数组对应的是一块连续的内存空间,这也是数组能够通过索引进行访问的前提,因为可以通过首地址+偏移量来定位到索引对应的内存空间。

遍历

遍历数组的方式主要有两种,一种是for+index的方式,一种是通过增强for循环进行遍历。

//数组初始化
int[] arr = new int[10];
Arrays.parallelSetAll(arr, i -> i);

//通过索引进行遍历
for (int i = 0;i < arr.length;i ++) {
   System.out.println(arr[i]);
}

//通过增强for循环进行遍历
for (int value : arr) {
   System.out.println(value);
}

Arrays工具类

Arrays是java.util包下的一个用来操作数组的工具类,主要包括以下几种类型的操作

  • sort/paralleSort
  • binarySearch

  • parallePrefix

  • fill

  • copyOf/copyOfRange

  • asList

  • equals/hashCode/toString

  • spliterator

sort/paralleSort能够实现排序的功能,可以对基本数据类型进行排序,也可以对引用数据类型排序,在对引用数据类型进行排序是需要满足一定的要求:即实现了Comparable接口或指定Comparator比较器,比如下面是其中一个sort方法的注解:

Sorts the specified array of objects into ascending order, 
according to the natural ordering of its elements. 
All elements in the array must implement the Comparable interface

paralleSort是一种能够支持并行计算的排序,当数组长度达到1<<13(8192)时就会触发并行机制,具体的是采用归并排序算法,而并行任务是通过ForkJoin线程池来驱动,下面赋上内部的注释说明:

The sorting algorithm is a parallel sort-merge that breaks the array into sub-
arrays that are themselves sorted and then merged. When the sub-array length 
reaches a minimum granularity, the sub-array is sorted using the appropriate 
Arrays.sort method. If the length of the specified array is less than the minimum 
granularity, then it is sorted using the appropriate Arrays.sort method. The 
algorithm requires a working space no greater than the size of the specified range 
of the original array. The ForkJoin common pool is used to execute any parallel tasks

binraySearch方法简单理解就是通过二分查找从目标数组中找到目标值,但它提供了很多重载方法,比如支持不同的数据类型的方法、在数组的指定区间内进行查找等,在对应用类型的数组中的数据进行查找时,数组内的元素必须实现Comparable接口。

parallePrefix是在jdk1.8引进的一个方法,来看一下方法签名

public static <T> void parallelPrefix(T[] array, BinaryOperator<T> op) ;

具体的作用看下面的例子:

//经过初始化后,原数组为[0,1,2]
int[] arr = new int[3];
Arrays.parallelSetAll(arr, i -> i);

Arrays.parallelPrefix(arr, new IntBinaryOperator() {
     //left表示前一个值,right表示当前值
     @Override
     public int applyAsInt(int left, int right) {
                //这里的逻辑就是把前一个值和当前值进行加和操作
                return left + right;
    }
});

for (int i = 0;i < arr.length;i ++) {
    System.out.println(arr[i]);
}

经过上面的操作后,最终的数组结果是:0,1,3

fill方法就是对数组内容进行填充,支持不同种类型的数组,也支持在数组指定区间内填充数据。

public static void fill(short[] a, short val);
public static void fill(short[] a, int fromIndex, int toIndex, short val);
...

copyOf/copyOfRange提供复制数组中的数据的功能,copyOf的全量复制,copyOfRange能复制原数组中指定区间内的数据

public static long[] copyOf(long[] original, int newLength);
public static <T> T[] copyOfRange(T[] original, int from, int to);

asList是我最喜欢的一个方法,它可以把一个数组转换成Collection,然后利用Collection的Stream API可以很方便的堆数组中的数据进行各种操作,但坏处就是可能会产生比较多的临时对象,消耗内存空间。

public static <T> List<T> asList(T... a);

equals/hashCode/toString这几个方法都是针对数组的基础操作,equals比较两数组是否相等。hashCode将数组中的每个元素加入到计算hashCode的算法中来,最终得出一个更加合理的hashCode值。toString就是对数组的字符串输出进行了格式化,如果直接调用一个数组的toString方法的话,打印出来的其实是数组对象的内存地址,不具有可读性。

标签:arr,重学,JAVA,int,Arrays,数组,array,public
来源: https://blog.csdn.net/echoagod/article/details/121587168

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有