Home > Java > javaTutorial > body text

How to traverse each element in an array in java

WBOY
Release: 2023-05-11 20:40:04
forward
1242 people have browsed it

1. Array overview: An array is a container that stores multiple variables (elements). The data types of these multiple variables are the same.

2. Array concept:

Arrays store the same type of data. Data type A container with multiple elements

Arrays can store both basic data types and reference data types

3. Array definition format:

Format 1: Data type [] Array name (commonly used)

Format 2: Data type array name []

4. Array initialization:

  • Arrays in Java must Initialize first before use

  • The so-called initialization is to allocate memory space for the elements in the array and assign a value to each array element.

  • One kind of initialization, the initialization only specifies the length of the group, and the array is assigned an initial value by washing

  • The second type of initialization, the initial value of each element is specified during initialization, and the array length is determined by the system

  • Format: data type [] array name = new data type [array length];

  • The array length is actually the number of elements in the array Number.

- int[] arr = new int[3];

Explanation: A value of type int is defined, and 3 ints can be stored in this array Type of value.

Memory allocation in JAVA

  1. Stack; storage of local variables

  • Local variables are variables defined in methods or statements

  • Local variables are recycled immediately after use


2. Heap: stores new things

  • Every object has a first address value, which is a hexadecimal address

  • The data in each object has a default value

  • After use, it will be recycled by the garbage collector.


3. Method area

4. Common problems with array operations:

-Array index out-of-bounds exception, accessing an index value that does not exist in the array, such as int [ ] arr = new int[2]

This array exists arr[0] arr[1] arr[2]

If you output arr[3], it is an array out of bounds.

  • Null pointer exception, the array reference does not point to the object, but is operating on the elements in the object.

Exercise: Known array int[] arr= {11,22,33,44,55}

Traverse each element in the array.

public class Test05 {

<code>public static void main(String[] args) {	<br>int[] arr = { 11, 22, 33, 44, 55 };	<br>for (int x = 0; x < 5; x++) {

		System.out.println(arr[x]);

	}

}</code>
Copy after login

}

2 .Get the maximum value in the array

Principle of getting the maximum value of the array:

  • First use the first element in the array as a reference

  • Then get all the following elements and compare them with the reference object

  • If any element is larger than the reference object, leave it

  • After all comparisons, the largest element remains.


#public class Test05 {

<code>public static void main(String[] args) {	<br>int[] arr = { 11, 22, 33, 44, 55 };	<br>int max = arr[0];	<br>for (int x = 0; x < arr.length; x++) {		<br>if (max < arr[x]) {			<br>max = arr[x];

		}

	}
	System.out.println(max);
}</code>
Copy after login
}

The above is the detailed content of How to traverse each element in an array in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template