Table of Contents
Definition of array
Use of array
Common exceptions:
Home Java javaTutorial Example analysis of arrays and common exceptions in Java

Example analysis of arrays and common exceptions in Java

Apr 28, 2023 pm 07:34 PM
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 [];
//以上两种方法都可以
Copy after login
  • 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,
//但是数组的长度一旦被设置,就无法更改
Copy after login

2: Directly initialize the array

int [] number = {1,2,3,4,5,6}
//也可以直接用进行赋值,写入相同类型的数值即可
Copy after login

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])
Copy after login

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])
}
Copy after login
  • 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];
Copy after login

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]);
            }
        }
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

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

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

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

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

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

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

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

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

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

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

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

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

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

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

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

See all articles