创造新数组
初始化
import org. apache. commons. lang3. ArrayUtils;
# 正常初始化
String[ ] arr = new String[ ] { "zyy" , "zxx" , "zww" } ;
String[ ] arr = { "wyy" , "wzz" , "wxx" , "wxx" } ;
# ArrayUtils
String[ ] arr = ArrayUtils. toArray ( "a" , "b" , "c" , "d" , "e" ) ;
# Arrays
String[ ] arr = new String [ 4 ] ;
Arrays. fill ( arr , "wyy" ) ;
# Java8对数组的支持
int [ ] ints = IntStream. rangeClosed ( 3 , 9 ) . toArray ( ) ;
复制数组
# Object
String[ ] strings5 = arr. clone ( ) ;
# ArrayUtils
String[ ] strings6 = ArrayUtils. clone ( arr) ;
截取子数组
# Arrays
String[ ] arr = { "wyy" , "wzz" , "wxx" , "wxx" , "asd" } ;
String[ ] strings1 = Arrays. copyOf ( arr, 4 ) ;
String[ ] strings2 = Arrays. copyOfRange ( arr, 0 , 1 ) ;
String[ ] strings3 = Arrays. copyOfRange ( arr, 1 , arr. length) ;
String[ ] strings4 = Arrays. copyOfRange ( arr, 1 , arr. length - 1 ) ;
# ArrayUtils
String[ ] strings5 = ArrayUtils. subarray ( arr, 1 , 3 ) ;
合并数组
int [ ] intArray = { 1 , 2 , 88 , 3 , 4 , 5 } ;
int [ ] intArray2 = { 6 , 7 , 8 , 9 , 10 } ;
int [ ] combinedIntArray = ArrayUtils . addAll ( intArray, intArray2) ;
每隔n个元素便返回
public static int [ ] everyNth ( int [ ] elements, int nth) {
return IntStream . range ( 0 , elements. length) . filter ( i -> i % nth == nth - 1 ) . map ( i -> elements[ i] ) . toArray ( ) ;
}
null to []
String [ ] arr1 = null ;
String [ ] strings = ArrayUtils . nullToEmpty ( arr1) ;
遍历
数组遍历
int [ ] s5 = new int [ ] { 7 , 3 , 2 , 8 , 9 } ;
for ( Integer s : s5) {
System. out . print ( s + " " ) ;
}
for ( int i = 0 ; i < s5. length; i++ ) {
System. out . print ( s5[ i] + " " ) ;
}
下标
检查数组是否包含某个值
String[ ] stringArray = { "a" , "b" , "c" , "d" , "e" } ;
# Arrays
boolean b = Arrays. asList ( stringArray) . contains ( "a" ) ;
# ArrayUtils
boolean a = ArrayUtils. contains ( stringArray, "a" ) ;
查找元素下标
# 查找元素第一次出现的下标
Integer[ ] array1 = { 1 , 1 , 1 , 2 , 7 , 11 , 22 , 37 } ;
# Arrays
int i = Arrays. binarySearch ( array1, 2 ) ;
# ArrayUtils
int j = ArrayUtils. indexOf ( array1, 2 ) ;
int j = ArrayUtils. lastIndexOf ( array1, 2 ) ;
# 查找数组中元素的索引,在不存在元素的情况下返回-1
public static int indexOf ( int [ ] elements, int el) {
return IntStream. range ( 0 , elements. length) . filter ( idx -> elements[ idx] == el) . findFirst ( ) . orElse ( - 1 ) ;
}
# 查找数组中元素的最后索引,在不存在元素的情况下返回-1
public static int lastIndexOf ( int [ ] elements, int el) {
return IntStream. iterate ( elements. length - 1 , i -> i - 1 ) . limit ( elements. length) . filter ( idx -> elements[ idx] == el) . findFirst ( ) . orElse ( - 1 ) ;
}
# 查找所有元素下标
String[ ] stringArray = { "a" , "b" , "c" , "d" , "a" } ;
BitSet a = ArrayUtils. indexesOf ( stringArray, "a" ) ;
BitSet a = ArrayUtils. indexesOf ( arr, "a" , 2 ) ;
# 查找下标是否有效
ArrayUtils. isArrayIndexValid ( null , 0 )
ArrayUtils. isArrayIndexValid ( [ ] , 0 )
ArrayUtils. isArrayIndexValid ( [ "a" ] , 0 )
查找并返回两个同类型数组之间第一个不匹配的索引
int [ ] array1 = { 2 , 7 , 11 , 22 , 37 } ;
int [ ] array2 = { 2 , 7 , 11 , 22 , 37 } ;
int [ ] array3 = { 2 , 7 , 19 , 31 , 39 , 56 } ;
int index1 = Arrays . mismatch ( array1, array2) ;
int index2 = Arrays . mismatch ( array1, array3) ;
元素增删改查
插入元素
# Arrays
# ArrayUtils
# 从数组后添加元素
String[ ] stringArray = { "a" , "b" , "c" , "d" , "e" } ;
String[ ] fs = ArrayUtils. add ( stringArray, "f" ) ;
# 往数组里添加数组
String[ ] stringArray = { "a" , "b" , "c" , "d" , "e" } ;
String[ ] stringArray2 = { "a" , "b" , "c" , "d" , "e" } ;
String[ ] fs = ArrayUtils. addAll ( stringArray, stringArray2) ;
# 从数组前添加元素
String[ ] stringArray = { "a" , "b" , "c" , "d" , "e" } ;
String[ ] fs = ArrayUtils. addFirst ( stringArray, "t" ) ;
# 插入元素到指定位置
String[ ] stringArray = { "a" , "a" , "b" , "c" , "d" , "a" } ;
String[ ] zs = ArrayUtils. insert ( 1 , stringArray, "z" ) ;
移除数组中的元素
# Arrays
# ArrayUtils
# 删除指定位置元素 create a new array
ArrayUtils. remove ( [ "a" ] , 0 ) = [ ]
ArrayUtils. remove ( [ "a" , "b" ] , 0 ) = [ "b" ]
ArrayUtils. remove ( [ "a" , "b" ] , 1 ) = [ "a" ]
ArrayUtils. remove ( [ "a" , "b" , "c" ] , 1 ) = [ "a" , "c" ]
# 删除指定位置多个元素
ArrayUtils. removeAll ( [ 1 ] , 0 ) = [ ]
ArrayUtils. removeAll ( [ 2 , 6 ] , 0 ) = [ 6 ]
ArrayUtils. removeAll ( [ 2 , 6 ] , 0 , 1 ) = [ ]
ArrayUtils. removeAll ( [ 2 , 6 , 3 ] , 1 , 2 ) = [ 2 ]
ArrayUtils. removeAll ( [ 2 , 6 , 3 ] , 0 , 2 ) = [ 6 ]
ArrayUtils. removeAll ( [ 2 , 6 , 3 ] , 0 , 1 , 2 ) = [ ]
# 删除所有指定元素
ArrayUtils. removeElement ( null , true ) = null
ArrayUtils. removeElement ( [ ] , true ) = [ ]
ArrayUtils. removeElement ( [ true ] , false ) = [ true ]
ArrayUtils. removeElement ( [ true , false ] , false ) = [ true ]
ArrayUtils. removeElement ( [ true , false , true ] , true ) = [ false , true ]
元素顺序
逆向一个数组
# Arrays
# ArrayUtils
int [ ] intArray = { 1 , 2 , 3 , 4 , 5 } ;
ArrayUtils. reverse ( intArray) ;
移动数组的顺序
# Arrays
# ArrayUtils
# Shifts the order of the given int array
String[ ] stringArray1 = { "a" , "b" , "c" , "d" , "e" } ;
ArrayUtils. shift ( stringArray1, 1 ) ;
String[ ] stringArray = { "a" , "b" , "c" , "d" , "e" } ;
ArrayUtils. shift ( stringArray, 2 ) ;
String[ ] stringArray2 = { "a" , "b" , "c" , "d" , "e" } ;
ArrayUtils. shift ( stringArray2, 3 ) ;
String[ ] stringArray3 = { "a" , "b" , "c" , "d" , "e" } ;
ArrayUtils. shift ( stringArray3, 4 ) ;
String[ ] stringArray4 = { "a" , "b" , "c" , "d" , "e" } ;
ArrayUtils. shift ( stringArray4, 5 ) ;
String[ ] stringArray5 = { "a" , "b" , "c" , "d" , "e" } ;
ArrayUtils. shift ( stringArray5, 6 ) ;
交换数组两元素位置
# Arrays
# ArrayUtils
ArrayUtils . swap ( [ "1" , "2" , "3" ] , 0 , 2 ) -> [ "3" , "2" , "1" ]
ArrayUtils . swap ( [ "1" , "2" , "3" ] , 0 , 0 ) -> [ "1" , "2" , "3" ]
ArrayUtils . swap ( [ "1" , "2" , "3" ] , 1 , 0 ) -> [ "2" , "1" , "3" ]
ArrayUtils . swap ( [ "1" , "2" , "3" ] , 0 , 5 ) -> [ "1" , "2" , "3" ]
ArrayUtils . swap ( [ "1" , "2" , "3" ] , - 1 , 1 ) -> [ "2" , "1" , "3" ]
编辑元素
编辑每个元素
# Arrays
double [ ] a = { 1.0 , 2.0 , 3.0 , 4.4 } ;
Arrays. setAll ( a, x -> a[ x] * a[ x] ) ;
[ 1.0 , 4.0 , 9.0 , 19.360000000000003 ]
# ArrayUtils
Java8流式用法
Double [ ] stringArray = { 1D , 2D , 3D , 11D , 22D , 33D , 111D , 222D , 333D } ;
Arrays . stream ( stringArray) . filter ( a -> a > 2D ) . forEach ( aDouble -> System . out. print ( aDouble + " 、" ) ) ;
相等
数据equals
Integer[ ] array1 = { 1 , 1 , 1 , 2 , 7 , 11 , 22 , 37 } ;
Integer[ ] array2 = { 1 , 1 , 1 , 2 , 7 , 11 , 22 , 37 } ;
# Arrays
boolean equals = Arrays. equals ( array1, array2) ;
boolean equals = Arrays. equals ( array1, 2 , 4 , array2, 2 , 4 ) ;
boolean equals = Arrays. equals ( array1, array2, new Comparator< Integer> ( ) {
@Override
public int compare ( Integer o1, Integer o2) {
return ( o1 < o2 ? - 1 : ( o1. equals ( o2) ? 0 : 1 ) ) ;
}
} ) ;
boolean equals4 = Arrays. equals ( array1, array2, ( o1, o2) -> ( o1 < o2 ? - 1 : ( o1. equals ( o2) ? 0 : 1 ) ) ) ;
数据结构间切换
数组 to string
String[ ] s1 = { "wyy" , "wzz" , "wxx" , "wxx" } ;
# String
String str4 = String. join ( "," , s1) ;
# StringUtils
String str3 = StringUtils. join ( s1, "," ) ;
# Arrays
String str1 = Arrays. toString ( s1) ;
# ArrayUtils
String str2 = ArrayUtils. toString ( s1) ;
# java8
String str5 = Arrays. stream ( s1) . collect ( Collectors. joining ( "," ) ) ;
数组 to List
String[ ] s2 = { "wyy" , "wzz" , "wxx" , "wxx" } ;
# Arrays
List< String> list = Arrays. asList ( s2) ;
# java9
List< String> list = List. of ( s2) ;
# List
List< String> list = new ArrayList< > ( Arrays. asList ( s2) ) ;
List< String> list = new ArrayList< > ( List. of ( s2) ) ;
List to 数组
Integer [ ] list = integers2. toArray ( Integer [ ] :: new ) ;
Integer [ ] list = integers2. toArray ( new Integer [ 0 ] ) ;
Integer [ ] list = integers2. toArray ( new Integer [ integers2. size ( ) ] ) ;
字符串,数组,集合之间相互转换总结
数组 to Set
# set
Set< String> set = new HashSet< > ( Arrays. asList ( s2) ) ;
数组 to Map
Map colorMap = ArrayUtils . toMap ( new String [ ] [ ] {
{ "RED" , "#FF0000" } ,
{ "GREEN" , "#00FF00" } ,
{ "BLUE" , "#0000FF" }
} ) ;
输出
打印二维数组
int [ ] [ ] ints = new int [ ] [ ] { { 1 , 2 , 3 } , { 4 , 5 , 6 } } ;
Arrays. stream ( ints) . forEach ( a -> System. out . println ( Arrays. toString ( a) ) ) ;
[ 1 , 2 , 3 ]
[ 4 , 5 , 6 ]
字节数组
将整数转换为字节数组
byte [ ] bytes = ByteBuffer
. allocate ( i)
. putInt ( 1000 )
. array ( ) ;
# 传统
for ( byte t : bytes) {
System. out . format ( "0x%x " , t) ;
}
# java8
IntStream. range ( 0 , bytes. length) . forEach ( i -> System. out . format ( "0x%x " , bytes[ i] ) ) ;
4 字节表示: 0x0 0x0 0x3 0xe8
5 字节表示: 0x0 0x0 0x3 0xe8 0x0
6 字节表示: 0x0 0x0 0x3 0xe8 0x0 0x0
7 字节表示: 0x0 0x0 0x3 0xe8 0x0 0x0 0x0
8 字节表示: 0x0 0x0 0x3 0xe8 0x0 0x0 0x0 0x0
9 字节表示: 0x0 0x0 0x3 0xe8 0x0 0x0 0x0 0x0 0x0
以字节为单位返回字符串的长度
public static int byteSize ( String input) {
return input. getBytes ( ) . length;
}
基本方法
boolean a = ArrayUtils . isEmpty ( arr1) ;
boolean b = ArrayUtils . isNotEmpty ( arr1) ;
boolean b = ArrayUtils . isSameType ( arr1, arr2) ;
boolean b = ArrayUtils . isSameLength ( arr1, arr2) ;
boolean b = ArrayUtils . isSorted ( arr1) ;
参考
stackoverflow中票数最多的数组操作方法 斩草除根学ArrayUtils 斩草除根学Arrays