斩草除根学ArrayUtils

add -> 从数组后添加元素

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • T(泛型)

使用效果

String[] stringArray = {"a", "b", "c", "d", "e"};

System.out.println(Arrays.toString(stringArray));

String[] fs = ArrayUtils.add(stringArray, "f");
System.out.println(Arrays.toString(fs));

[a, b, c, d, e]
[a, b, c, d, e, f]

PS:

ArrayUtils.add(null, null)      = IllegalArgumentException
ArrayUtils.add(null, "a")       = ["a"]
ArrayUtils.add(["a"], null)     = ["a", null]
ArrayUtils.add(["a"], "b")      = ["a", "b"]
ArrayUtils.add(["a", "b"], "c") = ["a", "b", "c"]

addAll -> 往数组里添加数组

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • T(泛型)、T (泛型)多参数

使用效果

@Test public void addAllPrac() {
    String[] stringArray = {"a", "b", "c", "d", "e"};
    String[] stringArray2 = {"a", "b", "c", "d", "e"};
    String[] fs = ArrayUtils.addAll(stringArray, stringArray2);
    System.out.println(Arrays.toString(fs));
}

[a, b, c, d, e, a, b, c, d, e]

ps

ArrayUtils.addAll(array1, null)   = cloned copy of array1
ArrayUtils.addAll(null, array2)   = cloned copy of array2
ArrayUtils.addAll([], [])         = []

addFirst -> 从数组前添加元素

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • T(泛型)

使用效果

String[] stringArray = {"a", "b", "c", "d", "e"};

String[] fs = ArrayUtils.addFirst(stringArray, "t");
System.out.println(Arrays.toString(fs));

[t, a, b, c, d, e]

PS

ArrayUtils.addFirst(null, true)          = [true]
ArrayUtils.addFirst([true], false)       = [false, true]
ArrayUtils.addFirst([true, false], true) = [true, true, false]

clone -> 克隆数组

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • T(泛型)

使用效果

String[] stringArray = {"a", "b", "c", "d", "e"};
String[] fs = ArrayUtils.clone(stringArray);
System.out.println(Arrays.toString(fs));

[a, b, c, d, e]


contains -> 判断数组是否包含元素

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • Object
    注意double还有个指定范围内查询的方法

使用效果

String[] stringArray = {"a", "b", "c", "d", "e"};
boolean a = ArrayUtils.contains(stringArray, "a");
System.out.println(a);

true


get -> 获取下标对应的元素、另一个方法可以指定默认值

image.png

getLength

hashCode

image.png

indexesOf

->Finds the indices of the given value in the array

->Finds the indices of the given value in the array starting at the given index.

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • Object

使用效果

String[] stringArray = {"a", "b", "c", "d", "a"};
BitSet a = ArrayUtils.indexesOf(stringArray, "a");
System.out.println(a);

{0, 4}

String[] stringArray = {"a", "a", "b", "c", "d", "a"};
BitSet a = ArrayUtils.indexesOf(stringArray, "a");
System.out.println(a);

BitSet b = ArrayUtils.indexesOf(stringArray, "a", 1);
System.out.println(b);

{0, 1, 5}
{1, 5}

针对double的情况

// 查找3D这个值得所有下标
double[] arr = {1D, 2D, 3D, 4D, 5D, 2D};
BitSet e = ArrayUtils.indexesOf(arr, 2D);
System.out.println(e);

{1, 5}

// 从下标为1开始查找3D这个值得所有下标
double[] arr = {1D, 2D, 3D, 4D, 5D, 1D};
BitSet d = ArrayUtils.indexesOf(arr, 3D, 1);
System.out.println(d);
BitSet d1 = ArrayUtils.indexesOf(arr, 3D, 2);
System.out.println(d1);
BitSet d2 = ArrayUtils.indexesOf(arr, 3D, 3);
System.out.println(d2);

{2}
{2}
{}

// 找出3-1,3,3+1的下标的位置
double[] arr = {1D, 2D, 3D, 4D, 5D, 2D};

BitSet c = ArrayUtils.indexesOf(arr, 3D, 1D);
System.out.println(c);

{1, 2, 3, 5}

// 从下标为1开始查找、找出3-1,3,3+1的下标的位置
double[] arr = {1D, 2D, 3D, 4D, 5D, 2D};
BitSet f = ArrayUtils.indexesOf(arr, 3D, 2, 1D);
System.out.println(f);

{2, 3, 5}


indexof : 查找元素第一次出现的下标

-> Finds the index of the given object in the array.

-> Finds the index of the given object in the array starting at the given index.

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • Object
String[] stringArray = {"a", "a", "b", "c", "d", "a"};
int a = ArrayUtils.indexOf(stringArray, "a");
System.out.println(a);

0

String[] stringArray = {"a", "a", "b", "c", "d", "a"};

int b = ArrayUtils.indexOf(stringArray, "a",1);
System.out.println(b);

1


lastIndexOf

-> Finds the last index of the given value within the array.

-> Finds the last index of the given value in the array starting at the given index.

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • Object

insert ->Inserts elements into an array at the given index (starting from zero).

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • T(泛型)
ArrayUtils.insert(index, null, null)      = null
ArrayUtils.insert(index, array, null)     = cloned copy of 'array'
ArrayUtils.insert(index, null, values)    = null

使用效果

 String[] stringArray = {"a", "a", "b", "c", "d", "a"};
 String[] zs = ArrayUtils.insert(1, stringArray, "z");
 System.out.println(Arrays.toString(stringArray));
 System.out.println(Arrays.toString(zs));

[a, a, b, c, d, a]
[a, z, a, b, c, d, a]

String[] stringArray = {"a", "a", "b", "c", "d", "a"};
String[] zs = ArrayUtils.insert(1, stringArray, "z", "s");
System.out.println(Arrays.toString(stringArray));
System.out.println(Arrays.toString(zs));

[a, a, b, c, d, a]
[a, z, s, a, b, c, d, a]


isArrayIndexValid ->Returns whether a given array can safely be accessed at the given index.

image.png
ArrayUtils.isArrayIndexValid(null, 0)       = false
ArrayUtils.isArrayIndexValid([], 0)         = false
ArrayUtils.isArrayIndexValid(["a"], 0)      = true

使用效果

 String[] stringArray = {"a", "a"};
 boolean arrayIndexValid = ArrayUtils.isArrayIndexValid(stringArray, 2);
 System.out.println(arrayIndexValid);

false


isEmpty ->Checks if an array of primitive booleans is empty or null

isNotEmpty ->Checks if an array of primitive booleans is not empty and not null

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • Object
  • T(泛型)

使用效果

String[] stringArray = {"a", "a"};
boolean a = ArrayUtils.isEmpty(stringArray);
boolean b = ArrayUtils.isNotEmpty(stringArray);
System.out.println(a);
System.out.println(b);

false
true


isSameLength -> Checks whether two arrays are the same length, treating arrays as length 0

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • Object 一个为元素类型、一个就为Object

isSameType -> Checks whether two arrays are the same type taking into account multi-dimensional arrays.

image.png

isSorted ->

This method checks whether the provided array is sorted according to natural ordering
false before true

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • T(泛型)

使用效果

String[] stringArray = {"a", "a"};
boolean a = ArrayUtils.isSorted(stringArray);
System.out.println(a);
String[] arr = {"a", "b"};
boolean b = ArrayUtils.isSorted(arr);
System.out.println(b);
String[] arr1 = {"a", "b", "a"};
boolean c = ArrayUtils.isSorted(arr1);
System.out.println(c);

true
true
false


nullToEmpty -> Defensive programming technique to change a null reference to an empty one.

  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • Byte、Short、Integer、Long、Float、Double、Boolean、Character
  • T(泛型)
  • Object
  • Class

remove

-> Removes the element at the specified position from the specified array. All subsequent elements are shifted to the left
-> Removes the elements at the specified positions from the specified array. All remaining elements are shifted to the left.


image.png
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) = []

removeAllOccurrences -> Removes the occurrences of the specified element from the specified int array.

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • T(泛型)

removeElement -> Removes the first occurrence of the specified element from thespecified array. All subsequent elements are shifted to the left. If the array doesn't contains such an element, no elements are removed from the array.

image.png
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]
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • T(泛型)

reverse -> Reverses the order of the given array in the given range.

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • Object

shift -> Shifts the order of the given int array.

->Shifts the order of the given int array.
->Shifts the order of a series of elements in the given int array.


image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • Object

使用效果

String[] stringArray1 = {"a", "b", "c", "d", "e"};
ArrayUtils.shift(stringArray1,1);
System.out.println(Arrays.toString(stringArray1));

String[] stringArray = {"a", "b", "c", "d", "e"};
ArrayUtils.shift(stringArray,2);
System.out.println(Arrays.toString(stringArray));

String[] stringArray2 = {"a", "b", "c", "d", "e"};
ArrayUtils.shift(stringArray2,3);
System.out.println(Arrays.toString(stringArray2));

String[] stringArray3 = {"a", "b", "c", "d", "e"};
ArrayUtils.shift(stringArray3,4);
System.out.println(Arrays.toString(stringArray3));

String[] stringArray4 = {"a", "b", "c", "d", "e"};
ArrayUtils.shift(stringArray4,5);
System.out.println(Arrays.toString(stringArray4));

String[] stringArray5 = {"a", "b", "c", "d", "e"};
ArrayUtils.shift(stringArray5,6);// 6%5 ->1 取模
System.out.println(Arrays.toString(stringArray5));

[e, a, b, c, d]
[d, e, a, b, c]
[c, d, e, a, b]
[b, c, d, e, a]
[a, b, c, d, e]
[e, a, b, c, d]

String[] arr1 = {"a", "b", "c", "d", "e"};
ArrayUtils.shift(arr1,1,3,1);
System.out.println(Arrays.toString(arr1));

String[] arr = {"a", "b", "c", "d", "e"};
ArrayUtils.shift(arr,1,3,2);
System.out.println(Arrays.toString(arr));

[a, c, b, d, e]
[a, b, c, d, e]


shuffle -> Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • Object

subarray -> Produces a new byte array containing the elements between the start and end indices. 左闭右开

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • T(泛型)

使用效果

String[] stringArray1 = {"a", "b", "c", "d", "e"};
String[] subarray = ArrayUtils.subarray(stringArray1, 1, 3);
System.out.println(Arrays.toString(subarray));

[b, c]


swap

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • Object

-> Swaps two elements in the given boolean array.

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"]

-> Swaps a series of elements in the given int array.

ArrayUtils.swap(["1", "2", "3", "4"], 0, 2, 1) -> ["3", "2", "1", "4"]
ArrayUtils.swap(["1", "2", "3", "4"], 0, 0, 1) -> ["1", "2", "3", "4"]
ArrayUtils.swap(["1", "2", "3", "4"], 2, 0, 2) -> ["3", "4", "1", "2"]
ArrayUtils.swap(["1", "2", "3", "4"], -3, 2, 2) -> ["3", "4", "1", "2"]
ArrayUtils.swap(["1", "2", "3", "4"], 0, 3, 3) -> ["4", "2", "3", "1"]

toArray -> Create a type-safe generic array.

image.png

使用效果

String[] strings = ArrayUtils.toArray("a", "b", "c", "d", "e");
System.out.println(Arrays.toString(strings));

[a, b, c, d, e]


toMap -> Converts the given array into a Map

Map colorMap = ArrayUtils.toMap(new String[][] {
    {"RED", "#FF0000"},
    {"GREEN", "#00FF00"},
    {"BLUE", "#0000FF"}});
System.out.println(colorMap);

{RED=#FF0000, GREEN=#00FF00, BLUE=#0000FF}


toObject -> Converts an array of primitive ints to objects.

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char

toPrimitive

-> Converts an array of object Booleans to primitives.

-> Converts an array of object Booleans to primitives handling null

image.png
  • byte、 short、 int、 long、 float、 double、 boolean 、char
  • Object

toString -> Outputs an array as a String, treating null as an empty array.

image.png

使用效果

String[] strings = ArrayUtils.toArray("a", "b", "c", "d", "e");
System.out.println(Arrays.toString(strings));
System.out.println(ArrayUtils.toString(strings));

[a, b, c, d, e]
{a,b,c,d,e}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 229,460评论 6 538
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 99,067评论 3 423
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 177,467评论 0 382
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 63,468评论 1 316
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 72,184评论 6 410
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 55,582评论 1 325
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 43,616评论 3 444
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,794评论 0 289
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 49,343评论 1 335
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 41,096评论 3 356
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 43,291评论 1 371
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,863评论 5 362
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,513评论 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,941评论 0 28
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 36,190评论 1 291
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 52,026评论 3 396
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 48,253评论 2 375

推荐阅读更多精彩内容