zoukankan      html  css  js  c++  java
  • 数组

    数组所创建的存储空间只能存放同一种类型的数据

    数组所分配的空间是连续的

    数组所分配的空间是不可扩充的

    数组定义和创建

    数组定义:int[] a; int a[]; int a[][]; int[]a[]; int [][]a;

    数组创建:a= new int[2]

    通过数组下标得到数组变量中的某个值:数组名[下标]   a[i]

    i的取值从0开始

    数组扩充

    1.把老数组的数据复制到新的数组中

    2.让老数组指向新数组的地址

    int a[]=new int[]{1,2,3}
    
    int b[]=new int[a.length*2];
    
    system.arraycopy(a,0,b,0,a.length);//把老数组的数据复制到新的数组中
    
    a=b;//让老数组指向新数组的地址
    
    //也可以使用for循环把老数组的数据复制到新的数组中

    数组初始化数据

    int[] scores=new int[3];
    scores[0]=90;
    scores[1]=80; scores[2]=100; String[] names=new String[]{"xiaoming","xiaoli","xiaozhang"};
    int []aa[]={{1,2,3},{4,5,6}};
    System.out.println(aa[0][1]);//2
    

     一维数组排序及遍历

    int[] a={11,2,35,4,7,1};
    Arrays.sort(a);
    for(int i=0;i<a.length;i++)
    {System.out.println(a[i]);}

    二维数组遍历

    int []aa[]={{1,2,3},{4,5,6}};

    System.out.println(aa[0][1]);//2

    for(int i=0;i<aa.length;i++)

    {  for(int j=0;j<aa[i].length;j++)

     {   System.out.println(aa[i][j]);

     }

    }

  • 相关阅读:
    Leetcode 第 210 场周赛
    Leetcode 834. 树中距离之和
    Leetcode 第36场双周赛
    力扣 第 208 场周赛
    cf 665 DMaximum Distributed Tree
    Codeforces Round #672 (Div. 2) A~D
    Educational Codeforces Round 95 (Rated for Div. 2) A~D
    CCF CSP 201612-3 权限查询
    Codeforces Round #669 (Div. 2) A~C
    201703-4 地铁修建
  • 原文地址:https://www.cnblogs.com/tianhao/p/4167164.html
Copyright © 2011-2022 走看看