博客
关于我
Array 数组操作
阅读量:286 次
发布时间:2019-03-01

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

系列文章目录


前言

本文简单介绍了如何利用Java自带的类创建数组,掌握这些将有助于在Leetcode中刷题。


Java

  1. 创建数组

    int[] a = {     1, 2, 3};int[] b = new int[]{     1, 2, 3};int[] c = new int[3];for (int i = 0; i < a.length; i++) {         c[i] = i + 1;}ArrayList
    arr = new ArrayList<>();for (int i = 0; i < 3; i++) { arr.add(i + 1);}
  2. 添加元素

    arr.add(99);arr.add(3, 88);		// 在索引为3的位置添加元素88
  3. 访问元素

    int c1 = c[1];int arr1 = arr.get(1);
  4. 更新元素

    c[1] = 11;arr.set(1, 11);
  5. 移除元素

    arr.remove(3);	// 删除元素3
  6. 数组长度

    int cSize = c.length;int arrSize = arr.size();
  7. 遍历数组

    for (int i = 0; i < c.length; i++) {         int current = c[i];}for (int i = 0; i < arr.size(); i++) {         int current = arr.get(i);}
  8. 查找元素

    for (int i = 0; i < c.length; i++) {         if (c[i] == 99) {             System.out.println("We found 99 in c");    }}boolean is99 = arr.contains(99);
  9. 数组排序

    c = new int[]{     2, 3, 1};Arrays.sort(c);arr = new ArrayList<>{     };arr.add(2);arr.add(3);arr.add(1);Collections.sort(arr);		// 从小到大Collections.sort(arr, Collections.reverseOrder());	// 翻转,从大到小
  10. 打印数组

    System.out.println(array); 这样是不行的,这样打印是的是数组的首地址

    1. 一维数组

      int[] array = {       1, 2, 3, 4, 5};
      1. for 循环

        for (int i = 0; i < array.length; i++) {         	System.out.println(array[i]);}
      2. for each

        for (int a : array) {         	System.out.println(a);}
      3. Arrays的toString()

        调用Array.toString(a),返回一个包含数组元素的字符串,这些元素被放置在括号内,并用逗号分开

        System.out.println(Arrays.toString(array));
    2. 二维数组

      int[][] arrays = {           {       16, 3, 2, 13},    {       5, 10, 11, 8},    {       9, 6, 7, 3}};
      1. for 循环

        for (int i = 0; i < arrays.length; i++) {             for (int j = 0; i < arrays[0].length; i++) {                 System.out.print(array[i][j] + " ");    }	System.out.println();}
      2. for each

        for (int[] a : arrays) {         	for (int b : a) {                 System.out.print(b + " ");	}    System.out.println();}
      3. Arrays的toString()

        for (int i = 0; i < array.length; i++) {         	System.out.println(Arrays.toString(arrays[i]));}

总结

参考资料来源:

转载地址:http://tgxo.baihongyu.com/

你可能感兴趣的文章
nginx 集群配置方式 静态文件处理
查看>>
Nginx+Django-Python+BPMN-JS的整合工作流实战项目
查看>>
Nginx+Keepalived+LVS集群实战
查看>>
Nginx+Keepalived实现简单版高可用主备切换
查看>>
nginx+mysql+redis+mongdb+rabbitmq 自动化部署脚本
查看>>
nginx+php的搭建
查看>>
nginx+tomcat+memcached
查看>>
Nginx+Tomcat实现动静分离
查看>>
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>
nginx+vsftp搭建图片服务器
查看>>
Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
nginx-vts + prometheus 监控nginx
查看>>
Nginx/Apache反向代理
查看>>
Nginx: 413 – Request Entity Too Large Error and Solution
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>
Nginx:objs/Makefile:432: recipe for target ‘objs/src/core/ngx_murmurhash.o‘解决方法
查看>>
Nginx、HAProxy、LVS
查看>>