zoukankan      html  css  js  c++  java
  • 暑假自学(9)

    Java中的数组:

    一维数组:
    1.数组的声明与初始化:
      1.1静态初始化:
        int[] 数组名;
        数组名 = new int[]{元素};
      1.2动态初始化
        String[] names = new String[5];
    2.数组的长度:
      数组名.length
    3.数组默认初始化值
      整形:0
      char型:ASCII码值为0的字符
      boolean型:false
      String(引用数据类)型:null


    二维数组:
    1.声明与初始化:
      1.1静态初始化:
        int[][] 数组名 = new int[][]{};
      1.2动态初始化:
        String[][] 数组名 = new String[3][4];或[3][];

        String[][] Array = new String[3][];
        Array[1] = new String[4];
      1.3数组长度
        [][]看外层
      1.4二维数组的遍历:
      for(int i=0;i<arr.length;i++)
      {
        for(int j=0;j<arr[i].length;j++)
        {
          System.out.print(arr[i][j] + " ");
        }
      }
      1.5二维数组的初始值:
      当定义时指定长度:
      以int arr[][] = new int[3][4]为例
      arr[2] = 地址值;
      arr[1][3] = 0;
      当未指定长度时:
      以int arr[][] = new int[3][]为例
      arr[3][] = null;
      arr[1][2]报错(错误指针);

    栈和堆:
    栈中主要存放局部变量(在main中的变量都可称作局部变量)
    堆中存放new的变量

    代码样例(杨辉三角):

    public class project01 {
      public static void main(String[] args) {
        int [][] a = new int [10][];
        for(int i=0;i<a.length;i++)
        {
          a[i] = new int[i+1];
          a[i][0] = 1;
          a[i][i] = 1;
        }
        for(int i=0;i<a.length;i++)
        {
          for(int j=0;j<a[i].length;j++)
          {
            if(j!=0&&i!=j)
            {
              a[i][j] = a[i-1][j] + a[i-1][j-1];
            }
            System.out.print(a[i][j] + " ");
          }
          System.out.println();
        }

      }
    }

  • 相关阅读:
    bzoj1178/luogu3626 会议中心 (倍增+STL::set)
    suoi31 最近公共祖先2 (倍增lca)
    luogu4155/bzoj4444 国旗计划 (倍增)
    [BZOJ1864][Zjoi2006]三色二叉树
    [Luogu3070][USACO13JAN]岛游记Island Travels
    [Luogu2458][SDOI2006]保安站岗
    [BZOJ1191][HNOI2006]超级英雄Hero
    [BZOJ1050][HAOI2006]旅行
    [Luogu2973][USACO10HOL]赶小猪Driving Out the Piggi…
    [BZOJ1833][ZJOI2010]数字计数
  • 原文地址:https://www.cnblogs.com/buxiang-Christina/p/13300451.html
Copyright © 2011-2022 走看看