How arrays work in Java (illustration)

Array

An array is an expression that prepares a box for storing data side by side and puts it in and out. There are two types of arrays: one-dimensional arrays and multidimensional arrays. I will explain the mechanism of the array using an example.

One-dimensional array

Let's substitute 1,2,3,4,5 for each element of the array and display it.

Array1



class Array {
	public static void main(String[] args) {

		int[] a = new int[5];

		for (int i = 0; i < a.length; i++){
			a[i] = i + 1;
		}

		for (int i = 0; i < a.length; i++){
			System.out.println("a[" + i + "] = " + a[i]);
		}
	} 
}
【Execution result】
a[0]=1
a[1]=2
a[2]=3
a[3]=4
a[4]=5

Qiita用_配列1.PNG

Multidimensional array

Let's substitute 1,2,3,4,1,2,3,4 for each element of the array and display it.

Array2


class A2 {
	public static void main(String[] args) {

			int[][] x = new int[2][4];

			for (int i = 0; i < 2; i++) {
				for (int j = 0; j < 4; j++) {
					x[i][j] = j + 1;
					System.out.println("x[" + i + "][" + j + "] = " + x[i][j]);
				}
			}
	} 
} 
【Execution result】
x[0][0] = 1
x[0][1] = 2
x[0][2] = 3
x[0][3] = 4
x[1][0] = 1
x[1][1] = 2
x[1][2] = 3
x[1][3] = 4

Qiita用_配列2.PNG

Recommended Posts

How arrays work in Java (illustration)
Combine arrays in Java
How to learn JAVA in 7 days
How to use classes in Java?
[Processing × Java] How to use arrays
How to concatenate strings in java
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
How to do base conversion in Java
How to implement coding conventions in Java
How to get the date in java
[Java] About arrays
Changes in Java 11
Rock-paper-scissors in Java
About Java arrays
How to display a web page in Java
How to get Class from Element in Java
How to hide null fields in response in Java
[Java] How to substitute Model Mapper in Jackson
How to solve an Expression Problem in Java
How to write Java String # getBytes in Kotlin?
How much ternary operator is allowed in Java
[java] sort in list
Read JSON in Java
Make Blackjack in Java
Java, about 2D arrays
How to call functions in bulk with Java reflection
How to create a Java environment in just 3 seconds
[Java] How to omit the private constructor in Lombok
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
"Hello World" in Java
Callable Interface in Java
How to input / output IBM mainframe files in Java?
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
How to create a data URI (base64) in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
How to generate / verify ID token in Java Memo
POST JSON in Java
How to convert a file to a byte array in Java
How to Git manage Java EE projects in Eclipse
Date manipulation in Java 8
What's new in Java 8
Summary of how to implement default arguments in Java
Work as a team! Face Null head-on in Java
How to put old Java (8 series) in macOS 10.15 Catalina
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java