Studying Java-Part 7-Array

1.First of all

This time it will be an array. An array is a group that can treat multiple variables as a group.

In other words, I think it's like having multiple boxes in one big box.

キャプチャ_24.png

Such an image.

2. Declaration of array

Now let's see how to declare an array.

** Type [] Array name **

It's like adding [] to the type when declaring a variable.

You also have to prepare how many boxes of variables the array will use. The fixed phrase at this time is

** Array name = new type [number]; **

The type of the right-hand side should be the same as the type of the array specified at the time of declaration. When dealing with classes, the types of the right side and the left side may be different, but it will be complicated, so I will omit it this time.

Also, the number is the number of boxes you want to use. If you specify 3, it's like making 3 boxes.

Here is an example source.

Main.java


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

		//Array declaration
		int[] array;

		//Generate
		array = new int[ 3 ];

	}
}

Now, I would like to actually assign a value to the box in the array, but before that ...

Accessing the boxes in the array is a bit tricky.

As mentioned above, if 3 pieces are generated, the numbers 0 to 2 will be assigned.

キャプチャ_25.png

Use this number to access. This number is called "** subscript **".

Note that ** starts with **.

With this in mind, take a look at the following sources:

Main.java


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

		//Array declaration
		int[] array;

		//Generate 3
		array = new int[3];

		//Substitute 64 for array 0
		array[0] = 64;

		//Assign 128 to array # 1
		array[1] = 128;

		//In array 2(No. 0+No. 1)Substitute
		array[2] = array[0] + array[1];

	}
}

If you want to use the 0th in the array, write [0] next to the array name, like array [0].

Now, I will explain the source.

First, we declare an array called array and generate three boxes.

Then I assigned the 0th value of 64 and the 1st value of 128 in the array.

キャプチャ_26.png

Also, the value (192), which is the sum of the 0th (64) and the 1st (128), is assigned to the 2nd.

キャプチャ_27.png

It is the flow of processing.

3. Initialization

You can assign the values in the array at the same time as the declaration. Initialization with a feeling like int number = 100 ;.

Let's actually source.

Main.java


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

		//Array declaration&Initialization
		int[] array = { 64, 128, 256 };

	}
}

Use {} like this. Also, separate them with ",".

In the case of this source, the number of boxes in the array is not specified, but three are prepared. The reason is that there are three literals in {}.

This means that the number of boxes used in the array will be generated according to the number of literals between {}.

4. Multidimensional array

So far, I've only used one []. An array with only one [] is called a "** 1 dimensional array **".

On the other hand, if there are multiple [], it is called a "** multidimensional array **", and if there are two [], it is called a two-dimensional array. This time, as a representative, I would like to deal with a two-dimensional array.

A two-dimensional array is, so to speak, a combination of two arrays that can be handled by a single array name.

Now, let's actually generate 3 boxes with 2 arrays with the array name array.

Main.java


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

		//Declaration of multidimensional array
		int[][] array;

		//2 arrays generate 3 boxes
		array = new int[ 2 ][ 3 ];

	}
}

There are two [] s in the declaration. This is a declaration of a two-dimensional array.

Also, there are two [] on the right side when creating an array. Match the number with [] at the time of declaration.

[2] [3] means that 6 boxes of 2x3 will be generated.

キャプチャ_28.png

An image of a table like this.

Also, to access the position in each array, it will be as follows.

Main.java


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

		//Declaration of multidimensional array
		int[][] array;

		//2 arrays generate 3 boxes
		array = new int[ 2 ][ 3 ];

		//Substitute in box
		array[1][2] = 100;

	}
}

I tried substituting 100 for [2] of [1] of the array. At this time, the image in the table below. キャプチャ_29.png

[1] points to the vertical number 1 and [2] points to the horizontal number 2. We call them "row" and "column" respectively.

The place where the above 100 is entered is the position pointed to by [1] [2].

Also, I will introduce how to initialize at the same time as the declaration. I would like to use the 2x3 array shown in the example.

Main.java


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

		//Declaration of multidimensional array&Initialization
		int[][] array = { { 100, 200, 300 }, { 101, 201, 301 } };

	}
}

In this way, {} is like enclosing two {}. The more dimensions there are, the more {}.

キャプチャ_30.png

Also, don't forget to put a "," between the inner {100, 200, 300} and {101, 201, 301}.

5. Conclusion

Arrays are treated as reference types. The details are about the memory address, which tends to be complicated, so I omitted it.

If you study C language etc., you will deepen your understanding of addresses, but at first it may be difficult to get started, so it is not bad to study C language etc. after studying Java first. I think it's a route.

Also, if you use an array, you can handle related variables together, and if you use it together with the iterative statement introduced later, you can create many merits.

It's an important "array", so don't forget it.

Next time, I will talk about "cast".

Next time → "Study Java-Part 8-Cast"

Recommended Posts

Studying Java-Part 7-Array
Studying Java-Part 0-Overview
Studying Java-Part 4-Literal
Studying Java-Part 5-Constant
Studying Java-Part 9-Scope
Studying Java-Part 3-Type
Studying Java-Part 10-if statement
Studying Java-Part 1-Hello World
Studying Java-Part 11-switch statement
Array
Array
Studying Java ―― 3
[Java] array
[Ruby] Array
Studying Java ―― 9
Java array
java (array)
Studying Java ―― 4
Studying Java -5
Java array
Studying Java ―― 1
[Java] Array
Studying Java # 0
Array practice 2
Studying Java ―― 8
Java array
Studying Java ②
12 of Array
Studying Java ―― 2
Studying Java ①
java array
[Java] Array
Studying Java -10