博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java成神之——集合框架之Array
阅读量:4332 次
发布时间:2019-06-06

本文共 4366 字,大约阅读时间需要 14 分钟。

Array

初始化

声明数组的几种方式    int[] arr = new int[3];     int[] arr = { 1, 2, 3 };    int[] arr = new int[] { 1, 2, 3 };     int[][] arr = { { 1, 2 }, { 3, 4, 5 } };    int[][] arr = new int[5][];     int[][] arr = new int[5][4];初始化    int[] arr = {1, 2, 3};    List
list = Arrays.asList(arr); Integer[] arr = {1, 2, 3}; List
list = Arrays.asList(arr);指定数组元素类型 int[] arr = (int[]) Array.newInstance(int.class, 3); arr[0] = 0; arr[1] = 1; arr[2] = 2;

填充元素的几种方式

第一种    int[] arr = new int[3];    Arrays.fill(arr, 1);                        // [1,1,1]第二种    int[] arr = new int[3];    Arrays.fill(arr, 0, 1, 2);                  // [2, 0, 0]第三种    int[] arr = new int[5];    Arrays.setAll(arr, i -> 2 * i);             // [0,2,4,6,8]第四种    ArrayList
arr = new ArrayList<>(); String[] str = {"a", "b", "c"}; arr.addAll(Arrays.asList(str)); 第五种 ArrayList
arr = new ArrayList<>(); String[] str = {"a", "b", "c"}; Collections.addAll(arr, str);第六种 int[] arr = {1, 2, 3}; List
list = Arrays.stream(arr).boxed().collect(Collectors.toList());第七种 String[] arr = {"a", "b", "c"}; List
list = Arrays.stream(arr).collect(Collectors.toList());第八种 List
list = new ArrayList<>(Arrays.asList("a", "b"));

数组转流

第一种    String[] str = new String[] {"a", "b", "c"};    Stream
stream = Arrays.stream(str); 第二种 Stream
stream = Stream.of("a", "b", "c");

遍历

第一种    for (int i = 0; i < array.length; i++) {        array[i] = i;    }第二种    for (int e : array) {        System.out.println(e);    }第三种    Integer[] boxed = {1, 2, 3};    Iterable
boxedIt = Arrays.asList(boxed); Iterator
fromBoxed1 = boxedIt.iterator(); while(fromBoxed1.hasNext()) { System.out.println(fromBoxed1.next()); }第四种 int[] primitives = {1, 2, 3}; IntStream primitiveStream = Arrays.stream(primitives); PrimitiveIterator.OfInt fromPrimitive1 = primitiveStream.iterator(); while(fromPrimitive1.hasNext()) { System.out.println(fromPrimitive1.next()); }

数组转成字符串

int[] arr = {1, 2, 3, 4, 5};Arrays.toString(arr);

排序

int[] array = {7, 4, 2, 1, 19};Arrays.sort(array); // [1,2,4,7,19]Integer[] array = {7, 4, 2, 1, 19};Arrays.sort(array, 0, array.length, Collections.reverseOrder()); // [19,7,4,2,1]

查找

二分查找    String[] str = new String[] { "a", "b", "c" };    int index = Arrays.binarySearch(str, "b");查找索引位置    int index = Arrays.asList(str).indexOf("b");使用流链式查找索引位置    int index = IntStream                .range(0, str.length)                .filter(i -> "a".equals(str[i]))                .findFirst()                .orElse(-1);contains是否存在    boolean isPresent = Arrays.asList(str).contains("b");使用流判断是否存在    boolean isPresent = Stream.of(str).anyMatch(x -> "a".equals(x));

数组扩大

String[] str = new String[] { "a", "b", "c" };String[] newstr = Arrays.copyOf(str, str.length + 1);newstr[str.length] = "d";

原始类型数组和包装类型数组转换

原始类型转成包装类型int[] primitiveArray = {1, 2, 3, 4};Integer[] boxedArray = Arrays.stream(primitiveArray).boxed().toArray(Integer[]::new);包装类型转成原始类型Integer[] boxedArray = {1, 2, 3, 4};int[] primitiveArray = Arrays.stream(boxedArray).mapToInt(Integer::intValue).toArray();

移除元素

String[] str = new String[]{"a", "b", "c"};List
list = new ArrayList<>(Arrays.asList(str));list.remove("a");

比较数组是否相等

String[] str1 = new String[]{"a", "b", "c"};String[] str2 = new String[]{"a", "b", "c"};Arrays.equals(str1, str2);

克隆

浅克隆第一种    int[] a = { 4, 1, 3, 2 };    int[] b = a.clone();第二种    int[] a = {4, 1, 3, 2};    int[] b = Arrays.copyOf(a, a.length);第三种    int[] a = { 4, 1, 3, 2 };    int[] b = new int[a.length];    System.arraycopy(a, 0, b, 0, a.length);第四种    int[] a = { 4, 1, 3, 2 };    int[] b = Arrays.copyOfRange(a, 0, a.length);

类型转换

Integer[] arr = { 1, 2, 3 };Number[] numbers = Arrays.copyOf(arr, arr.length, Number[].class);

过滤元素

List
list = new ArrayList
();list.add("a");list.add("b");list.add("c");List
filteredList = list.stream().filter(item -> !"b".equals(item)).collect(Collectors.toList());list.removeIf(item -> "b".equals(item));

结语

本文章是java成神的系列文章之一如果你想知道,但是本文没有的,请下方留言我会第一时间总结出来并发布填充到本文

转载于:https://www.cnblogs.com/ye-hcj/p/9723756.html

你可能感兴趣的文章
507 LOJ 「LibreOJ NOI Round #1」接竹竿
查看>>
UI基础--烟花动画
查看>>
2018. 2.4 Java中集合嵌套集合的练习
查看>>
精通ASP.NET Web程序测试
查看>>
vue 根据不同属性 设置背景
查看>>
51Nod1601 完全图的最小生成树计数 Trie Prufer编码
查看>>
Codeforces 1110D. Jongmah 动态规划
查看>>
android驱动在win10系统上安装的心酸历程
查看>>
优雅的程序员
查看>>
oracle之三 自动任务调度
查看>>
Android dex分包方案
查看>>
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>