Java Arrays class consists exclusively of static methods that operates on array.
Java Arrays類僅由對數組進行操作的靜態方法組成。
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類包含支持原始數據類型的重載方法。 這些方法是: Let’s have a look at the Arrays methods example through some programs.
讓我們通過一些程序看一下Arrays方法示例。
數組Java?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 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.
stringutil。 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 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");}}}
import java.util,Output of above program is:
上面程序的輸出是:
Element is found at the index :1
Element is found at the index :5
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.
java中的util。 下面是一個比較數組的簡單程序。
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
What if the array is inside of another array? Will Arrays equal method is capable to compare nested arrays?
如果該數組在另一個數組內部怎么辦? 數組相等方法是否可以比較嵌套數組?
javautil包,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.
javautil有哪些類、 我們可以看到,盡管兩個數組相等并且包含相同數量的元素,但是程序的輸出為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
java定義類數組、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
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态