Java Arrays class consists exclusively of static methods that operates on array.

Java Arrays类仅由对数组进行操作的静态方法组成。

Java数组 (Java Arrays)

  1. Java Arrays class was introduced in Java 1.2 as a utility class to perform various common operations on arrays in java.Java Arrays类是Java 1.2中引入的实用程序类,用于在Java中对数组执行各种常见操作。
  2. This class is part of java collections framework.此类是java collections框架的一部分。
  3. Java Arrays class contains methods for sorting, searching and comparing arrays.Java Arrays类包含用于排序,搜索和比较数组的方法。
  4. Arrays class also contains a static method that returns a list backed by the specified array.Arrays类还包含一个静态方法,该方法返回一个由指定数组支持的列表。
  5. Arrays class contains overloaded methods that support primitive data types. These methods are:
    public static <T> List<T> asList(T… a)
    public static void sort(int[] a)
    public static int binarySearch(int[] a, int k)
    public static boolean equals(int[] a, int[] a2)

    Arrays类包含支持原始数据类型的重载方法。 这些方法是:

Java阵列范例 (Java Arrays Examples)

Let’s have a look at the Arrays methods example through some programs.

让我们通过一些程序看一下Arrays方法示例。

Java数组asList (Java Arrays asList)

Java Arrays provides asList method that returns a list backed by the specified array. Below is a simple program for showing array as a List.

Java数组提供了asList方法,该方法返回由指定数组支持的列表 。 下面是一个简单的程序,用于将数组显示为列表。

package com.journaldev.examples;import java.util.Arrays;
import java.util.List;/*** Java Arrays Example Program* * @author pankaj**/public class ArraysAsListExample {public static void main(String[] args) {String[] strings = {"one", "two", "three", "four", "five"};// strings array is converted into a ListList<String> list = Arrays.asList(strings);System.out.println(list);}
}

Output of above program is:

上面程序的输出是:

[one, two, three, four, five]

Java数组排序 (Java Arrays sort)

Java Arrays provides sort method that sorts the element of specified array and also sorts the specified range of the given array into ascending order.
Below is a simple program for sorting arrays in java.

Java Arrays提供了sort方法,该方法可以对指定数组的元素进行排序,还可以将给定数组的指定范围按升序排序。
下面是一个用于在Java中对数组进行排序的简单程序。

package com.journaldev.examples;import java.util.Arrays;/*** Java Arrays Example Program* * @author pankaj**/
public class ArraysSortExample {public static void main(String[] args) {// Sorting Characterchar[] chars = {'B', 'D', 'C', 'A', 'E'};Arrays.sort(chars);System.out.print("Sorted Characters : ");for (char character : chars) {System.out.print(character+" ");}// Sorting Integerint[] integers = {5, 2, 1, 4, 3};Arrays.sort(integers);System.out.print("\nSorted Integers : ");for (int i : integers) {System.out.print(i+" ");}// Sorting Specific Range of Integersint[] ints = {5, 2, 1, 4, 3, 9, 6, 8, 7, 10};int fromIndex = 2;int toIndex = 7;Arrays.sort(ints, fromIndex, toIndex);System.out.print("\nSorted Integers of Specific Range : ");for (int i : ints) {System.out.print(i+" ");}}}

Output of above program is:

上面程序的输出是:

Sorted Characters : A B C D E
Sorted Integers : 1 2 3 4 5
Sorted Integers of Specific Range : 5 2 1 3 4 6 9 8 7 10

Java数组binarySearch (Java Arrays binarySearch)

Java Arrays binarySearch method uses binary search algorithm to search specified value from the elements of specified array and also searches from the specified range of the given array.

Java Arrays BinarySearch方法使用二进制搜索算法从指定数组的元素中搜索指定值,并从给定数组的指定范围中进行搜索。

package com.journaldev.examples;import java.util.Arrays;/*** Java Arrays binarySearch* * @author pankaj**/
public class ArraysBinarySearchExample {public static void main(String[] args) {// Searching a value from array of integerint[] integers = { 5, 2, 1, 4, 3, 9, 6, 8, 7, 10 };int index = Arrays.binarySearch(integers, 2);if (index >= 0) {System.out.println("Element is found at the index :" + index);} else {System.out.println("Element is not found");}// Searching a value from array of integer with specific rangeint fromIndex = 2;int toIndex = 7;int index2 = Arrays.binarySearch(integers, fromIndex, toIndex, 9);if (index2 >= 0) {System.out.println("Element is found at the index :" + index2);} else {System.out.println("Element is not found");}}}

Output of above program is:

上面程序的输出是:

Element is found at the index :1
Element is found at the index :5

Java数组等于 (Java Arrays equals)

Java Arrays provides equals method that is used to compare two arrays of the same type and returns boolean result. It returns true if two given arrays are equal to one another.

Java数组提供了equals方法,该方法用于比较两个相同类型的数组并返回布尔结果。 如果两个给定的数组彼此相等,则返回true。

Below is a simple program to compare arrays.

下面是一个比较数组的简单程序。

package com.journaldev.examples;import java.util.Arrays;/*** Java Arrays Example Program* * @author pankaj**/
public class ArraysEqualExample {public static void main(String[] args) {// Compare two arrays of type integer which are equalint[] a1 = { 1, 2, 3 };int[] a2 = { 1, 2, 3 };boolean equal = Arrays.equals(a1, a2);if (equal) {System.out.println("Arrays a1 and a2 are equal with Result : " + equal);    }else {System.out.println("Arrays a1 and a2 are not equal with Result : " + equal);}// Compare two arrays of type integer which are not equalint[] b1 = { 1, 2, 3 };int[] b2 = { 4, 5, 6 };boolean equal1 = Arrays.equals(b1, b2);if (equal1) {System.out.println("Arrays b1 and b2 are equal with Result : " + equal1);   }else {System.out.println("Arrays b1 and b2 are not equal with Result : " + equal1);}}}

Output of above program is given below.

上面程序的输出如下。

Arrays a1 and a2 are equal with Result : true
Arrays b1 and b2 are not equal with Result : false

比较嵌套数组 (Compare Nested Arrays)

What if the array is inside of another array? Will Arrays equal method is capable to compare nested arrays?

如果该数组在另一个数组内部怎么办? 数组相等方法是否可以比较嵌套数组?

Let’s have a look at the below example program.

让我们看一下下面的示例程序。

package com.journaldev.examples;import java.util.Arrays;/*** Java Arrays Example Program* * @author pankaj**/
public class ArraysNestedArrayExample {public static void main(String[] args) {// Compare two nested arrays of type integer which are equalint[] a1 = { 1, 2, 3 };int[] a2 = { 1, 2, 3 };Object[] b1 = {a1};Object[] b2 = {a2};boolean equal = Arrays.equals(b1, b2);if (equal) {System.out.println("Arrays b1 and b2 are equal with Result : " + equal);} else {System.out.println("Arrays b1 and b2 are not equal with Result : " + equal);}}}

Output of above program is:

上面程序的输出是:

Arrays b1 and b2 are not equal with Result : false

We can see that the output of the program is false despite the two arrays are equal and contains same number of elements.
Actually equal method is not able to compare nested arrays, for that Arrays have another method called deepEquals.

我们可以看到,尽管两个数组相等并且包含相同数量的元素,但是程序的输出为false。
实际上equal方法不能比较嵌套数组,因为该数组还有另一个称为deepEquals的方法。

Let’s have a look at the below example program.

让我们看一下下面的示例程序。

package com.journaldev.examples;import java.util.Arrays;/*** Java Arrays Example Program* * @author pankaj**/
public class ArraysNestedArrayExample {public static void main(String[] args) {// Compare two nested arrays of type integer which are equalint[] a1 = { 1, 2, 3 };int[] a2 = { 1, 2, 3 };Object[] b1 = {a1};Object[] b2 = {a2};boolean equal = Arrays.deepEquals(b1, b2);if (equal) {System.out.println("Arrays b1 and b2 are equal with Result : " + equal);} else {System.out.println("Arrays b1 and b2 are not equal with Result : " + equal);}}}

Output of above arrays equals program is:

上述数组等于程序的输出为:

Arrays b1 and b2 are equal with Result : true

That’s all for java Arrays class, I hope nothing important got missed here.

Java Arrays类就这些了,我希望这里没有重要的事情。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/16770/java-arrays-java-util-arrays

Java数组– java.util.Arrays相关推荐

  1. java数组转list(Arrays .asList)

    习惯性的错误代码: Integer[] intArr = {1,2,3}; List<Integer> lst = Arrays .asList(intArr); lst.add(4); ...

  2. JAVA数组Java StringBuffer 和 StringBuilder 类

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_34173549/article/details/80215173 Java StringBuf ...

  3. java数组_Java数组

    java数组 Java Array is a container that can hold a fixed number of values of the same type. The values ...

  4. Java基础篇--Java 数组

    Java基础篇--Java 数组 Java 数组 声明数组变量 创建数组 处理数组 For-Each 循环 数组作为函数的参数 数组作为函数的返回值 多维数组 多维数组的动态初始化(以二维数组为例) ...

  5. java数组搞笑_面试官:小伙子,给我说一下Java 数组吧

    Java 数组 Java 语言中提供的数组是用来存储固定大小的同类型元素. 1.声明数组变量 首先必须声明数组变量,才能在程序中使用数组. dataType[] arrayRefVar; // 首选的 ...

  6. 【java】Java教程

    文章目录 教程 简介 主要特性 Java 语言是简单的: Java 语言是面向对象的: Java语言是分布式的: Java 语言是健壮的: Java语言是安全的: Java 语言是体系结构中立的: J ...

  7. 菜鸟教程中Java语法(Java教程+Java面向对象)

    Java基本数据类型 Java变量类型 Java运算符 Java循环语句 Java条件语句 Java switch case Java Number & Math类 Java Characte ...

  8. java的知识点17——java.util.Arrays类、多维数组

    java.util.Arrays类 Arrays类包含了:排序.查找.填充.打印内容等常见的操作. 打印数组 package cn.dym08; import java.util.Arrays;pub ...

  9. 要想数组用的 6,怎能不懂 java.util.Arrays

    java.util.Arrays 类就是为数组而生的专用工具类,基本上常见的对数组的操作,Arrays 类都考虑到了,这让我由衷地觉得,是时候给该类的作者 Josh Bloch.Neal Gafter ...

最新文章

  1. 02.Web大前端时代之:HTML5+CSS3入门系列~H5结构元素
  2. 如何将本地代码上传到GitHub
  3. 【机器学习】特征提取代码汇总
  4. CSS Sprite “精灵图“
  5. nginx反向代理和shiro权限校验产生的404问题
  6. python如何使用ppip安装xlwt_Python-xlwt库的基本使用
  7. JDK源码解析之 Java.lang.StringBuilder
  8. Qt QJson遍历
  9. 【操作篇】qBittorrent下载+转种Transmission快校版+IYUU Plus辅种教程
  10. 【65】如何通过sys文件系统remove和probe一个PCI设备
  11. linux计划任务5个*分表代表什么,Linux定时任务Crontab命令
  12. win10计算机本地组策略编辑器,win10本地组策略编辑器找不到怎么办_win10电脑没有gpedit.msc的解决办法...
  13. 如何安装cadence 软件
  14. 【学习小记】支配树【图论】
  15. linux防火墙关闭 重启 开启、防火墙开放端口
  16. HTML简易会员登录页面
  17. 2019年的计划,做最好的自己。
  18. RSA算法和DH算法的区别
  19. 建立高效的团队,可以做这七件事
  20. 【翻译】WannaCry ransomware attack

热门文章

  1. [转载] python oct_Python oct()
  2. [转载] Python3 * 和 ** 运算符
  3. [转载] python中numpy.concatenate()函数的使用
  4. MVC+WebApi+Restful
  5. Java匹马行天下之学编程的起点——编程常识知多少
  6. What is Closure
  7. jmeter正则中常见的转义字符-笔记三
  8. 44 The shopping psychology 购物心理
  9. Leetcode: 2. Add Two Numbers
  10. POJ-2078 Matrix,暴力枚举!