Example analysis of arrays and common exceptions in Java
Definition of array
1: A single variable can store information
2: Used to store data sets with the same data type. You can use a common name to refer to the data stored in the array. .
Features
Arrays can store any type of data, including primitive data types and reference data types, but once the type of the array is specified, it can only be used to store data of the specified type.
Use of array
Declare an array variable to store the array
Syntax
Data type[] Array name
Data type array name[]
//声明一个int类型 名为 numebr 的数组 int [] number; int number []; //以上两种方法都可以
Create a new array object and assign it to the value created in the previous step Array variables:
1: Use the new operator
int [] number = new [5]; // 意思是把number这个数组的长度设置为5, //但是数组的长度一旦被设置,就无法更改
2: Directly initialize the array
int [] number = {1,2,3,4,5,6} //也可以直接用进行赋值,写入相同类型的数值即可
3: Access or modify the storage in this array Data
a: If you need to access the elements in the array, you need to use the array name [subscript] form to access
b: Note that the maximum subscript of the array is: the length of the array -1 ;The initial subscript is: 0
c: The length of the array can be obtained by using the length attribute of the array. Each array object will have a length attribute to represent the length of the array, that is, the number of array elements.
//输出number数值第2个数字 System.out.println(number[1])
4: Traverse the array:
If you want to output all elements in the array to one side, you can use a for loop
int [] numebr = {1,2,3,4,5} for(int i=0;i<number.length;i++){ System.out.println(number[i]) }
Create Two-dimensional array
There are also two ways to create a two-dimensional array:
Method 1:
Use the new operator
int [][] number = new int[5][5];
Method 2: Declare the array and assign an initial value
int [][] nuber = {{1,2,3,},{4,5,6,},{7,8}}; //二维数组就是在一个大的数组内,还包含了二级数组,可以理解成嵌套的意思 //若要输出二维数组,也可以采用多个for循环的方式 int [][] nuber = {{1,2,3},{4,5,6},{7,8,9}}; for(int i =0;i<nuber.length;i++){ for (int j = 0;j<nuber.length;j++){ System.out.println(nuber[i][j]); } }
Issues to note:
Array out-of-bounds exception error:
Array out-of-bounds is a runtime exception, when the array is accessed When the length exceeds the length of the array itself, an array out-of-bounds exception will be thrown java.lang.ArrayIndexOutOfBoundsException
Array pointing and passing
The essence of reference passing is in stack memory **field (collection)** points to data in heap memory. When an array assignment or an array function parameter is passed, a value reference is passed, that is, the pointer to the data is passed so that it points to the same block of data memory, thereby achieving the purpose of array transfer. Therefore, when data is heaped in memory, other references pointing to the data segment will be modified.
Common exceptions:
Array subscript out-of-bounds exception:ArrayIndexOutOfBoundsException
String subscript out-of-bounds exception:StringIndexOutOfBoundsException
Class conversion exception:ClassCastException
NullPointerException:NullPointerException
Number formatting exception:NumberFormatException
Input cannot match exception:InputMismatchException
Parse exception:ParseException
No such element exception: NoSuchElementException
File not found exception:FileNotFoundException
Arithmetic exception:ArithmeticException
Wrong parameter exception:IllegalArgumentException
The above is the detailed content of Example analysis of arrays and common exceptions in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
