###Arrays类概述
java.util.Arrays类包含一个静态工厂,它允许将数组视为列表。以下是关于Arrays的重要观点
以下是java.util.Arrays类的声明 -
public class Arrays
extends Object
java.util.Arrays类提供了很多方法,但在本指南中,我们将介绍经常使用的和重要的Arrays类方法。
例子。在这个例子中,我们将把字符串数组转换为字符串类型的ArrayList。
import java.util.Arrays;
import java.util.List;
//*/*
/* This class shows different methods to convert Array to ArrayList
/*
/* @author javaguides.net
/*
/*/
public class ArrayToArrayList {
public static void main(String[] args) {
String anArrayOfStrings[] = { "Agra", "Mysore", "Chandigarh", "Bhopal" };
List<String> strList = Arrays.asList(anArrayOfStrings);
System.out.println("Original ArrayList from Arrays.asList()");
//* Display array list /*/
strList.forEach(str -> System.out.println(" " + str));
// change the array element and see the effect is propogated to list
// also.
anArrayOfStrings[0] = "Dehli";
System.out.println("\nChange in array effect on ArrayList");
//* Display array list /*/
strList.forEach(str -> System.out.println(" " + str));
}
}
输出
Original ArrayList from Arrays.asList()
Agra
Mysore
Chandigarh
Bhopal
Change in array effect on ArrayList
Dehli
Mysore
Chandigarh
Bhopal
让我们再看一个例子,将整数数组转换为整数类型的ArrayList。
Integer anArrayOfIntegers[] = { 1,2,3,4,5,6 };
List<Integer> intList = Arrays.asList(anArrayOfIntegers);
//* Display array list /*/
intList.forEach(str -> System.out.println(" " + str));
输出
1
2
3
4
5
6
Arrays类提供了许多重载的search()方法,使用二进制搜索算法在指定的数组中搜索指定的对象。
import java.util.Arrays;
import java.util.Date;
import java.util.List;
//*/*
/* java.util.Arrays Class API examples
/*
/* @author javaguides.net
/*
/*/
public class ArraysJavaUtilClass {
public static void main(String[] args) {
// Searches the specified array for the specified String using the
// binary search algorithm.
final String key = "abc";
String[] strArray = { "abc", "cdf", "pqr" };
int index = Arrays.binarySearch(strArray, key);
System.out.println(" String key found at index : " + index);
// Searches the specified array of ints for the specified value using
// the binary search algorithm.
int[] intArray = { 1, 2, 3, 4 };
index = Arrays.binarySearch(intArray, 3);
System.out.println(" String key found at index : " + index);
// Searches the specified array of bytes for the specified value using
// the binary search algorithm.
byte k = 1;
byte[] byteArray = { 1, 2, 3, 4, 5 };
Arrays.binarySearch(byteArray, k);
// Searches the specified array of chars for the specified value using
// the binary search algorithm.
char charKey = 'a';
char[] charArray = { 'a', 'b', 'c' };
Arrays.binarySearch(charArray, charKey);
// Searches the specified array of doubles for the specified value using
// the binary search algorithm.
double[] doubleArray = { 0.1, 0.2, 0.3 };
Arrays.binarySearch(doubleArray, 0.2);
// Searches the specified array of longs for the specified value using
// the binary search algorithm.
long[] longArray = { 1, 2, 3, 4, 5 };
Arrays.binarySearch(longArray, 1);
// Searches the specified array of floats for the specified value using
// the binary search algorithm
float[] floatArray = { 1, 2, 3, 4, 5 };
Arrays.binarySearch(floatArray, 2);
}
}
import java.util.Arrays;
//*/*
/* This class shows different methods for copy array in java
/* @author javaguides.net
/*
/*/
public class JavaArrayCopyExample {
public static void main(String[] args) {
int[] source = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
System.out.println("Source array = " + Arrays.toString(source));
int[] dest = Arrays.copyOf(source, source.length);
System.out.println(
"Copy First five elements of array. Result array = " + Arrays.toString(dest));
}
}
输出:
Source array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Copy First five elements of array. Result array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
String[] strArray1 = { "abc", "cdf", "pqr" };
String[] strArray2 = { "abc", "cdf", "pqr" };
System.out.println("Two Arrays Deep Equals :: " + Arrays.deepEquals(strArray1, strArray2));
输出
Two Arrays Deep Equals :: true
public class ArraysJavaUtilClass {
public static void main(String[] args) {
// Assigns the specified int value to each element of the specified
// array of ints.
int[] fillArray = new int[5];
System.out.printf("fillArray (before): %s\n", Arrays.toString(fillArray));
Arrays.fill(fillArray, 1);
System.out.printf("fillArray (after): %s", Arrays.toString(fillArray));
}
}
输出
fillArray (before): [0, 0, 0, 0, 0]
fillArray (after): [1, 1, 1, 1, 1]
例子。
import java.util.Arrays;
import java.util.Date;
import java.util.List;
//*/*
/* java.util.Arrays Class API examples
/*
/* @author javaguides.net
/*
/*/
public class ArraysJavaUtilClass {
public static void main(String[] args) {
// Sorts the specified array into ascending numerical order.
int[] intArray = { 3, 1, 2, 4 };
System.out.println("Original Array : " + Arrays.toString(intArray));
Arrays.sort(intArray);
System.out.println("Sorted Array : " + Arrays.toString(intArray));
// Sorts the specified array into ascending numerical order.
byte[] byteArray = { 1, 3, 2, 1, 5 };
System.out.println("Original Array : " + Arrays.toString(byteArray));
Arrays.sort(byteArray);
System.out.println("Sorted Array : " + Arrays.toString(byteArray));
// Sorts the specified array into ascending numerical order.
char[] charArray = { 'a', 'd', 'c','b' };
System.out.println("Original Array : " + Arrays.toString(charArray));
Arrays.sort(charArray);
System.out.println("Sorted Array : " + Arrays.toString(charArray));
// Sorts the specified array into ascending numerical order.
double[] doubleArray = { 0.1, 0.3, 0.2 };
System.out.println("Original Array : " + Arrays.toString(doubleArray));
Arrays.sort(doubleArray);
System.out.println("Sorted Array : " + Arrays.toString(doubleArray));
// Sorts the specified array into ascending numerical order.
long[] longArray = { 1, 3, 2, 5, 4 };
System.out.println("Original Array : " + Arrays.toString(longArray));
Arrays.sort(longArray);
System.out.println("Sorted Array : " + Arrays.toString(longArray));
// Sorts the specified array into ascending numerical order.
float[] floatArray = { 1.1f, 1.5f, 1.4f };
System.out.println("Original Array : " + Arrays.toString(floatArray));
Arrays.sort(floatArray);
System.out.println("Sorted Array : " + Arrays.toString(floatArray));
}
}
输出。
Original Array : [3, 1, 2, 4]
Sorted Array : [1, 2, 3, 4]
Original Array : [1, 3, 2, 1, 5]
Sorted Array : [1, 1, 2, 3, 5]
Original Array : [a, d, c, b]
Sorted Array : [a, b, c, d]
Original Array : [0.1, 0.3, 0.2]
Sorted Array : [0.1, 0.2, 0.3]
Original Array : [1, 3, 2, 5, 4]
Sorted Array : [1, 2, 3, 4, 5]
Original Array : [1.1, 1.5, 1.4]
Sorted Array : [1.1, 1.4, 1.5]
String内部实现了Comparable接口。
String[] strArray = { "abc", "cdf", "pqr" };
System.out.println("Original Array : " + Arrays.toString(strArray));
Arrays.sort(strArray);
System.out.println("Sorted Array : " + Arrays.toString(strArray));
输出:
Original Array : [abc, cdf, pqr]
Sorted Array : [abc, cdf, pqr]
Date内部实现了Comparable接口。注意,LocalDate类来自Java 8。
LocalDate[] dates = { LocalDate.now(), LocalDate.of(2017, 12, 12) };
System.out.println("Original Array : " + Arrays.toString(dates));
Arrays.sort(dates);
System.out.println("Sorted Array : " + Arrays.toString(dates));
输出:
Original Array : [2018-08-13, 2017-12-12]
Sorted Array : [2017-12-12, 2018-08-13]
。
//*/*
/* java.util.Arrays Class API examples
/*
/* @author javaguides.net
/*
/*/
public class ArraysJavaUtilClass {
public static void main(String[] args) {
// Sorts the specified array into ascending numerical order.
int[] intArray = { 3, 1, 2, 4 };
System.out.println(" int Array toString : " + Arrays.toString(intArray));
// Sorts the specified array into ascending numerical order.
byte[] byteArray = { 1, 3, 2, 1, 5 };
System.out.println(" byte Array toString : " + Arrays.toString(byteArray));
// Sorts the specified array into ascending numerical order.
char[] charArray = { 'a', 'd', 'c', 'b' };
System.out.println(" char Array toString : " + Arrays.toString(charArray));
// Sorts the specified array into ascending numerical order.
double[] doubleArray = { 0.1, 0.3, 0.2 };
System.out.println(" double Array toString : " + Arrays.toString(doubleArray));
// Sorts the specified array into ascending numerical order.
long[] longArray = { 1, 3, 2, 5, 4 };
System.out.println(" long Array toString : " + Arrays.toString(longArray));
// Sorts the specified array into ascending numerical order.
float[] floatArray = { 1.1f, 1.5f, 1.4f };
System.out.println(" float Array toString : " + Arrays.toString(floatArray));
String[] strArray = { "abc", "cdf", "pqr" };
System.out.println("string Array toString : " + Arrays.toString(strArray));
}
}
输出
int Array toString : [3, 1, 2, 4]
byte Array toString : [1, 3, 2, 1, 5]
char Array toString : [a, d, c, b]
double Array toString : [0.1, 0.3, 0.2]
long Array toString : [1, 3, 2, 5, 4]
float Array toString : [1.1, 1.5, 1.4]
string Array toString : [abc, cdf, pqr]
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/08/java-util-arrays-class-api-guide.html
内容来源于网络,如有侵权,请联系作者删除!