zoukankan      html  css  js  c++  java
  • 005 Java数组


    目录


    数组的定义

    方式一

    int[ ]   arr = new int[3]
    

    方式二:在定义的时候同时赋值

    int[ ] arr = new int[ ]{1, 2, 3}
    // 需要注意,在赋值的时候,在后面一个中括号中不要写数组内的数据个数,如果写了会报错
    

    方式三

    int[ ] arr = {1, 2, 3}
    // 直接赋值,这种方式用的最多,最方便;
    

    JVM的内存划分

    JVM对自身的内存分为5个区域

    =============

    • 寄存器:内存和CPU相关
    • 本地方法栈:JVM调用系统功能

    =============

    • 方法和数据共享:运行时期class文件存储
    • 方法栈:方法运行的时候,进入的内存
    • 堆:存储的容器和对象

    数组的赋值

    int[ ]   arr = new int[3]
    arr[0] = 1;
    

    数组的遍历和获取最值

    package day002;
    
    
    public class arrayDemo {
    
           public static void main(String[] args){
    
                  test001(); //遍历数组
    
                  test002(); //获取数组最大值
           }
    
           public static void test001(){
    
                  int[] arr = new int[]{1, 2, 3};
                  
                  for(int i = 0; i < 3 ; i++){
                         System.out.println(arr[i]);
                  }
           }
    
           public static void test002(){
           
                  int[] arr2 = new int[]{1, 5, 3};
                  
                  int max = arr2[0];
                  
                  for(int j = 1; j<arr2.length; j++){
                  
                         if(max < arr2[j]){
                         
                               max = arr2[j];
                         }
                  }
                  System.out.println(max);
           }
    }
    

    数组常见异常

    • 越界:java.lang.ArrayIndexOutOfBoundsException
    • 空指针:java.lang.NullPointerException

    二维数组和多维数组

    // 方式一
    int [ ] [ ] arr = new int[3] [4];
    
    // 方式二
    int [ ] [ ] = { {1, 2}, {1, 6}};
    

    说明:二维或者多维数组实际上是一个数组内存储的每个对象,是数组;

  • 相关阅读:
    数据库导出_导入
    vc6如何调试带参数的main函数
    配置mes
    vc调试大全
    C#中out和ref之间的区别
    TSQL 控制流语句
    删除文件夹及文件夹内的文件
    数据库 存储 研究方向 关键词
    apache 监听多端口
    js 产生随机数
  • 原文地址:https://www.cnblogs.com/chenadong/p/13064955.html
Copyright © 2011-2022 走看看