博客
关于我
java中Arrays的用法
阅读量:469 次
发布时间:2019-03-06

本文共 1346 字,大约阅读时间需要 4 分钟。

Java Arrays类常用方法解析

1. atList方法:返回一个固定大小的list

static <T> List<T> atList(T... a)

说明

该方法创建一个固定大小的List,并将给定的数组元素添加到其中。返回的List与原始数组共享元素,因此修改List时会影响数组。

应用示例

List
intList = Arrays.asList(1, 2, 3, 4);

2. binarySearch方法:折半查找法,返回所给元素的索引

static int binarySearch(int[] a, int fromIndex, int toIndex, int key)

说明

该方法在指定数组的某个范围内使用折半查找法查找指定的元素,返回其索引。如果元素不存在于指定范围内,返回-1。

应用示例

int index = Arrays.binarySearch(new int[] { 1, 2, 3, 4, 5, 6, 7 }, 3);

3. copyOf及copyOfRange方法:将一个数组拷贝到另一个数组中/或者其中的一部分

copyOf方法

static int[] copyOf(int[] original, int length)

说明

创建一个新数组,长度为指定的长度,并将原数组中的前length个元素拷贝到新数组中。若原数组长度小于length,新数组会填充默认值(0)。

copyOfRange方法

static int[] copyOfRange(int[] original, int from, int to)

说明

创建一个新数组,长度为原数组中从from到to-1的元素个数,并将该子数组拷贝到新数组中。from和to的范围必须满足from <= to。

应用示例

int[] a = {1, 2, 3, 4, 5};int[] b = Arrays.copyOf(a, 2);    // b = {1, 2}int[] c = Arrays.copyOfRange(a, 0, 3); // c = {1, 2, 3}

4. sort方法:排序 (升序)

static void sort(int[] a)

说明

对数组进行升序排序。可以通过传入fromIndex和toIndex参数,指定排序的开始和结束位置(左闭右闭)。

应用示例

int[] a = {7, 2, 5, 4, 3};Arrays.sort(a); // 排序后为 {2, 3, 4, 5, 7}

5. equals方法:比较两个数组

static boolean equals(int[] a, int[] b)

说明

判断两个数组是否相等。比较两个数组中的每个元素,直到找到一个不相等的元素或遍历完所有元素。

应用示例

int[] a = {7, 2, 5, 4, 3};int[] b = {1, 2, 3};System.out.println(a.equals(b)); // 输出: false

这些方法为Java程序员提供了对数组操作的强大工具,能够高效地完成数组的创建、排序、查找和拷贝等操作。

转载地址:http://mdnbz.baihongyu.com/

你可能感兴趣的文章
org.apache.dubbo.common.serialize.SerializationException: com.alibaba.fastjson2.JSONException: not s
查看>>
sqlserver学习笔记(三)—— 为数据库添加新的用户
查看>>
org.apache.http.conn.HttpHostConnectException: Connection to refused
查看>>
org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
查看>>
org.apache.ibatis.exceptions.PersistenceException:
查看>>
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
查看>>
org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
查看>>
org.apache.poi.hssf.util.Region
查看>>
org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
查看>>
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
查看>>
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'
查看>>
org.hibernate.HibernateException: Unable to get the default Bean Validation factory
查看>>
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
查看>>
org.springframework.amqp.AmqpConnectException:java.net.ConnectException:Connection timed out:connect
查看>>
org.springframework.beans.factory.BeanDefinitionStoreException
查看>>
org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
查看>>
org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
查看>>
SQL-CLR 类型映射 (LINQ to SQL)
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>