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<int[]> list = Arrays.asList(arr); Integer[] arr = {1, 2, 3};
List<Integer> 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<String> arr = new ArrayList<>();
String[] str = {"a", "b", "c"};
arr.addAll(Arrays.asList(str)); 第五种 ArrayList<String> arr = new ArrayList<>();
String[] str = {"a", "b", "c"};
Collections.addAll(arr, str); 第六种 int[] arr = {1, 2, 3};
List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList()); 第七种 String[] arr = {"a", "b", "c"};
List<String> list = Arrays.stream(arr).collect(Collectors.toList()); 第八种 List<String> list = new ArrayList<>(Arrays.asList("a", "b"));

数组转流

第一种

    String[] str = new String[] {"a", "b", "c"};
Stream<String> stream = Arrays.stream(str); 第二种 Stream<String> 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<Integer> boxedIt = Arrays.asList(boxed);
Iterator<Integer> 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<String> 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<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c"); List<String> filteredList = list.stream().filter(item -> !"b".equals(item)).collect(Collectors.toList()); list.removeIf(item -> "b".equals(item));

结语

本文章是java成神的系列文章之一

如果你想知道,但是本文没有的,请下方留言

我会第一时间总结出来并发布填充到本文

最新文章

  1. ZBrush中如何才能快速完成脸部雕刻(上)
  2. javaWeb开发模式
  3. JS初学之-循环生成坐标
  4. D&amp;F学数据结构系列——红黑树
  5. Spring RestTemplate介绍
  6. Java凝视Override、Deprecated、SuppressWarnings详细解释
  7. 【踩坑速记】MIUI系统BUG,调用系统相机拍照可能会带给你的一系列坑,将拍照适配方案进行到底!
  8. 修改nopCommerce中的实体
  9. IPython绘图和可视化---matplotlib
  10. SharePoint 2013 新特性 (三) 破改式 &mdash;&mdash; 设计管理器的使用 [1.设备通道]
  11. mysql 案例 ~ 函数汇总
  12. sql server紧急状态下登录脚本
  13. mac下VirtualBox跟linux虚拟机共享文件夹
  14. 在浏览器中运行java applet
  15. grid响应式布局
  16. iOS重签名脚本
  17. AndroidStudio-Error Please select Android SDK
  18. Mac OS X 10.10 执行 Eclipse 提示须要安装 Java
  19. latex02-LaTeX源文件的基本结构
  20. html 常用

热门文章

  1. lambda表达式----使用
  2. quartz简单应用
  3. 【scala】Option类型
  4. 如何拿到半数面试公司Offer——我的Python求职之路(转载)
  5. React typescript issue
  6. android开发之eclipse调试debug模式详解
  7. linux【基础命令】
  8. iOS6 自动布局 入门–Auto Layout
  9. Weex入门篇——Mac 安装Weex
  10. 【java规则引擎】java规则引擎搭建开发环境