数组的定义
1:单个变量能存储信息
2:用来存储具有相同数据类型的数据集合,可以使用共同的名字来引用数组中存储的数据。
特点
数组可以存储任何类型的数据,包括原始数据类型和引用数据类型,但是一旦指定了数组的类型之后,就只能用来存储指定类型的数据。
数组的使用
- 声明一个数组变量来存放该数组
java基础之数组中的常见异常,代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package com.atguigu.java; /* * 数组中的常见异常: * 1. 数组角标越界的异常:ArrayIndexOutOfBoundsExcetion * * 2. 空指针异常:NullPointerException * */ public class ArrayExceptionTest { public static void main(String[] args) { //1. 数组角标越界的异常:ArrayIndexOutOfBoundsExcetion int [] arr = new int []{ 1 , 2 , 3 , 4 , 5 }; // for(int i = 0;i <= arr.length;i++){ // System.out.println(arr[i]); // } // System.out.println(arr[-2]); // System.out.println("hello"); //2.2. 空指针异常:NullPointerException //情况一: // int[] arr1 = new int[]{1,2,3}; // arr1 = null; // System.out.println(arr1[0]); //情况二: // int[][] arr2 = new int[4][]; // System.out.println(arr2[0][0]); //情况三: String[] arr3 = new String[]{ "AA" , "BB" , "CC" }; arr3[ 0 ] = null ; System.out.println(arr3[ 0 ].toString()); } } |
补充:java 常见数组异常
1
2
3
4
5
6
7
|
数组中常见的异常 ArratIndexOutfBoundsException:数组角标越界异常 合理范围;[ 0 ,arr.length] 越界arr[- 1 ],arr[att.length] NullPointerExcetion:空指针异常 int [] arr= null ; arr[ 0 ] |
常见异常:
数组下标越界异常:ArrayIndexOutOfBoundsException
字符串下标越界异常:StringIndexOutOfBoundsException
类转换异常:ClassCastException
空指针异常:NullPointerException
数字格式化异常:NumberFormatException
输入无法匹配异常:InputMismatchException
解析异常:ParseException
没有这样的元素异常:NoSuchElementException
文件找不到异常:FileNotFoundException
算术异常:ArithmeticException
错误的参数异常:IllegalArgumentException
到此这篇关于Java十分钟掌握数组与常见异常的文章就介绍到这了,更多相关Java 数组内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_47481936/article/details/109548507