Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the usage of Java Array

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "case analysis of the use of Java array". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "case analysis of the use of Java array" can help you solve the problem.

I. Preface

Learning Overview: in the first eight days, we learned the basis of grammar, operators and expressions, loop structure, branch structure. Today, we mainly learn about the definition of arrays, related attribute methods, memory diagrams stored in arrays, and common errors.

Learning goal: master the two definition methods of array, related attributes, understand the principle of memory, error resolution

Second, the definition of array 1. Overview

If there is a class whose grades need to be stored, what method should be used?

As we learned before, you can define multiple variables to store different grades. But if there are more than 1000 students, is it possible to define more than 1000 variables? Of course not, we need to use our array.

two。 Static initialization array

Features: when defining an array, assign values to the array directly, and the length of the array is determined by the system.

Common format:

Data type [] array name = {element 1, element 2, element 3, … }

For example:

Int [] array= {1,2,3,4,5}

Double [] scores = {885,99.5,59.5}

3. Dynamically initialize array

Features: when defining an array, determine the type of the element and the length of the array, and then store the data

Common format:

Data type [] array name = new data type [length]

For example:

Int [] array= new int [5]

Double [] scores = new double [3]

Default value:

Data type specific definition type default value basic type

Byte 、 short 、 char 、 int 、 long

0

Float 、 double

0.0

Boolean

False reference type

Classes, interfaces, arrays, String

Null4. Summary

Arrays are suitable for large amounts of data of the same type

Static initialization is suitable for knowing element values

Dynamic initialization is suitable for not knowing which data to store.

Third, the attribute of the array. Visit

The general way to access an array is:

Array name [index]

Example:

/ / statically initialize the array int [] array= {1 array [0]); / / output 1System.out.println (array [1]); / / output 2System.out.println (array [3]); / / output 42. Length

Length you can call length directly to get the length of the array.

Example:

/ / statically initialize the array int [] array= {1 _ array.length _ 3 _ 4 _ 5}; System.out.println (array.length); / / call the method with the output length of 5pm / maximum index array.length-13. Ergodic

Traversal is an array element access, mainly used in search, data statistics.

We learned about the loop structure, the branch structure, and then iterate through an array through the for loop

Example:

Given the element {10pc8, 9, 4, 5, 6, 71, 2, 3, 9, 99}, use a static array to store and output elements greater than 5 in the array?

Coding implementation:

/ / statically initialize the array int [] array= {10, 8, 9, 4, 5, 5, 6, 8, 71, 2, 3, 9, 99}; for (int item0, System.out.println, i5) System.out.println (array [I]);}

Output result:

10 8 9 6 8 71 9 99

IV. Memory diagram

When the program is running, Java needs to allocate space in memory and divides the space into different regions.

Stack memory: stores local variables and disappears immediately after use

Heap memory: stores the contents (objects, entities) out of new. The address is reclaimed when the garbage collector is idle.

1. Single array memory graph

The following code to create an array implements its memory diagram

Coding implementation:

/ / dynamically initialize the array int [] arr=new int [3]; System.out.println (arr); System.out.println (arr [0]); System.out.println (arr [1]); System.out.println (arr [2]); / / modify the value arr [0] = 100 arr [2] = 200 arr System.out.println (arr); System.out.println (arr [0]); System.out.println (arr [1]); System.out.println (arr [2])

Output result:

[I@15db9742

0

0

0

[I@15db9742

one hundred

0

two hundred

Explain the principle:

Dynamic initialization first generates a new and an arr address value in heap memory, depending on the result of the compiler, assume 001. Due to dynamic initialization, each element has an initial value, as shown in the table above. We output the element, first access the array name and address, to the heap memory subscript, and then output the element value.

The process of modifying array values is the same as viewing, but with one more step of modification, as shown in the following figure:

two。 Most group memory graph

Multiple arrays and single arrays use the same principle of memory, so I won't talk too much about it here.

3. The array points to the same memory

If we change the address value of the two arrays to the same, what the modified result should be, as in the following code.

Coding implementation:

/ dynamically initialize the array int [] arr=new int [3]; arr [0] = 100 System.out.println arr [1] = 200 arr arr [2] = 300 System.out.println system. Out.println (arr); System.out.println (arr [0]); System.out.println (arr [1]); System.out.println (arr [2]); int [] arr2=arr;arr2 [0] = 111 System.out.println Arr2 [1] = 222 transarr2 [2] = 333 boot System.out.println (arr); System.out.println (arr [0]) System.out.println (arr2); System.out.println (arr2 [0])

Output result:

[I@15db9742

one hundred

two hundred

three hundred

[I@15db9742

one hundred and eleven

[I@15db9742

one hundred and eleven

Explain the principle:

The address of the first array in heap memory is 001, and the second array is also 001, so modifying the value of the second array is actually the same array memory. The value of the first array also changes, and the result is as follows:

Fifth, frequently asked questions 1. Index out-of-bounds / / static initialization array int [] array= {1, array 2, 3}; index (array [3])

After the above code runs, the following error exception occurs:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3

Explanation: we static the initial array to give 3 numbers, the maximum index is 2, when we access 3, we will report an error.

two。 Null pointer exception / / dynamically initialize array int [] array= new int [3]; array=null;System.out.println (array [0])

After the above code runs, the following error exception occurs:

Exception in thread "main" java.lang.NullPointerException

Explanation: we set the array to null so that the accessed array does not point to the data in the heap memory

This is the end of the content of "example Analysis of the usage of Java Array". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report