Java, arrays to start with beginners

Introduction

This article is a memorandum. Although it is a reference book level content, the code posted in this article is about ** Wrong ** are the main things. This is for the purpose of posting the part that was actually mistaken during coding and posting it for self-reflection. In addition, I will not touch on the deep part here because I will review it later while also studying the Java Silver exam questions.

environment

Language: Java11, JDK13.0.2 Operating environment: Windows 10

Array

** Array (ʻarray`) ** has the function of storing values of the same type together. To prepare the array, you need to follow the steps below.

  1. ** Declare an array variable **
  2. ** Reserve elements of the array (ʻelement`) **

Array preparation example


int[] arrayOfScore;
arrayOfScore = new int[40];
//Declaration of an array element that can store 40 int type values

//Or
int[] arrayOfScore = new int[40];

You can use the new operator to prepare an array container in memory. This new is handled again when it is a class or instance. In the example, 40 is specified, but the number in [] is a subscript (ʻindex) and is the serial number of the array prepared in the array variable. This ʻindex, ** Array ʻindex in Java starts at 0 ** Therefore, the maximum number is "number of elements-1". In the case of the example, ** from ** ʻarrayOfScore [0] to ʻarrayOfScore [39] is prepared, so even if you make a mistake, do not call it assuming that ʻarrayOfScore [40] exists.

Also, the array can be initialized with one line.

Array initialization


int[] arrayOfScore = {50, 62, 71, 94, 16, 8,};
//Now arrayOfScore[0] = 50;(Omitted below) is stored.

//The above equation is equal to the following equation.
int[] arrayOfScore = new arrayOfScore[6];
arrayOfScore[0] = 50;
arrayOfScore[1] = 62;
arrayOfScore[2] = 71;
arrayOfScore[3] = 94;
arrayOfScore[4] = 16;
arrayOfScore[5] = 8;

Regarding the question, "Because it is a variable, it can be assigned as a variable." Certainly it can be substituted, but there is one caveat. If you prepare another variable and assign an array variable to it, you will be "** referencing the same array from two variables **", so if you change the contents of one, the contents of the other will also be Looks strange. Keep in mind that we don't have another variable with the same value, we just ** refer to one source array **.

Multidimensional array

Java can create ** multidimensional arrays ** as arrays that span more than two dimensions, making the elements of the array into arrays. It's easy to understand if you imagine an Excel table.

Preparation of 2D array


int[][] eightClassesScoreArray;
 = new eightClassesScoreArray[40][8]
eightClassesScoreArray[0][0] = 65;
//(Omitted below) 

//You may do this
int[][] eightClassesScoreArray = {/*(abridgement)*/},{0,1,2,3,4 ,5 ,6 ,7}

Since the number of each element does not need to be the same, the above 40: 8 array is also possible, and even if the number of dimensions increases to 3D and 4D and the number of elements is different, it can exist as a multidimensional array.

Practice

arrayOmikuzi


//(abridgement)

int arrayOmikuziPattern[][] = new int[][5];
//For the time being, 5 stages have been decided from Daikichi.
//I want to see only the movement, so it's okay to leave the elements empty.

for(int checker = 0;checker <5;checker++)
{
System.out.println("Your fortune" + "【Unimplemented】" + "Came out. );
System.out.println("The result on the Omikuji side is" + (arrayOmikuziPattern[][checker]+1) + "It's your turn.");
}

Compile error when declaring a two-dimensional array. ** It seems that the number of elements in the first dimension should not be omitted when declaring a multidimensional array **.

At the end

I often hear that arrays are used for 3D data. I would like to take the time to understand the sequence again, as it may be directly linked to my future work.

reference

I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.

Easy Java 7th Edition Java SE11 Silver Problem Collection (commonly known as Kuromoto)

Recommended Posts

Java, arrays to start with beginners
Java to play with Function
Connect to DB with Java
Connect to MySQL 8 with Java
[Java] How to start a new line with StringBuilder
Java to learn with ramen [Part 1]
Dare to challenge Kaggle with Java (1)
I tried to interact with Java
Java, interface to start from beginner
A memo to start Java programming with VS Code (2020-04 version)
Java 8 ~ Stream API ~ to start now
[Processing × Java] How to use arrays
How to start Camunda with Docker
[For beginners] Minimum sample to update RecyclerView with DiffUtils in Java
How to compile Java with VsCode & Ant
[Java] How to compare with equals method
Learn Java with "So What" [For beginners]
Java 8 to start now ~ Date time API ~
[Java] Beginners want to make dating. 1st
Sample to start Ubuntu with Deployment with client-go
Easy to trip with Java regular expressions
Introduction to algorithms with java --Search (breadth-first search)
[Java] About arrays
About Java arrays
Corresponds to 17 arrays
Introduction to java
Java8 / 9 Beginners: Stream API addiction points and how to deal with them
Challenge to deal with garbled characters with Java AudioSystem.getMixerInfo ()
[Java] How to test for null with JUnit
Two ways to start a thread in Java + @
I tried to make Basic authentication with Java
Introduction to algorithms with java --Search (bit full search)
Deploy Java web app to Azure with maven
Try to link Ruby and Java with Dapr
I want to use java8 forEach with index
Getting started with Kotlin to send to Java developers
Try to implement TCP / IP + NIO with JAVA
Rails beginners tried to get started with RSpec
[Java] Article to add validation with Spring Boot 2.3.1.
Easy to make LINE BOT with Java Servlet
[Introduction to Java] Basics of java arithmetic (for beginners)
I tried to break a block with java (1)
Java Welcome to the Swamp of 2D Arrays
Install java with Homebrew
Java, about 2D arrays
How to call functions in bulk with Java reflection
List processing to understand with pictures --java8 stream / javaslang-
Submit a job to AWS Batch with Java (Eclipse)
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Combine arrays in Java
Install Java with Ansible
[Java 11] I tried to execute Java without compiling with javac
[Java] How to omit spring constructor injection with Lombok
[Java] Connect to MySQL
[For beginners] How to operate Stream API after Java 8
[Java] How to encrypt with AES encryption with standard library
Comfortable download with JAVA
`Failed to create the host-only adapter` with docker-machine start`
[Java] Refer to and set private variables with reflection
[For beginners] Minimum sample to display RecyclerView in Java