zoukankan      html  css  js  c++  java
  • 数组的常见问题

    Day05_SHJavaTraing_4-8-2017

    1.运行时异常:NullPointerException

      ①第一种体现形式  

    1 String[] strs = new String[4];
    2 strs = null;
    3 System.out.println(strs[0]);

      ②第二种体现形式(只要是null值,就不能去调用方法或者属性)

     1 String[] strs = new String[4];        
     2 strs[0] = "AAA";
     3 //toString()方法返回此数组元素的字符串表示形式
     4 System.out.println("strs[0]=" + strs[0].toString());
     5 
     6 /*
     7 因数组为引用数据类型数据,其元素默认初始化值为null,
     8 因此调用strs[1]的toString()方法会报NullPointerException异常
     9 */
    10 System.out.println("strs[1]=" + strs[1].toString());

     2.定义出错

     1 class ArrayDefinitionErrorDemo 
     2 {
     3     public static void main(String[] args) 
     4     {
     5         /*
     6         ①正确定义的第一种方式:int[] arr = new int[]{ 1, 2, 3 };
     7         ②正确定义的第二种方式:int[] arr = { 1, 2, 3 };
     8         */
     9         int[] arr;
    10         /*
    11         int[] arr;
    12         arr = {1,2,3};会报错
    13         正确书写格式:arr = new[]{1,2,3};
    14         */
    15         arr =  new int[]{1,2,3};
    16     }
    17 }
  • 相关阅读:
    luogu P3238 [HNOI2014]道路堵塞
    luogu P3235 [HNOI2014]江南乐
    luogu P3237 [HNOI2014]米特运输
    luogu P3233 [HNOI2014]世界树
    luogu P3234 [HNOI2014]抄卡组
    luogu P3250 [HNOI2016]网络
    luogu P3201 [HNOI2009]梦幻布丁
    luogu P4148 简单题
    luogu P3767 膜法
    luogu P4314 CPU监控
  • 原文地址:https://www.cnblogs.com/EzraOholiabXue/p/Day05_SHJavaTraing_4-8-2017_02.html
Copyright © 2011-2022 走看看