How to make a Java array

3 steps to make an array

Three steps are required to create an array.

  1. Declare variables in the array
  2. Create elements of the array
  3. Substitute a value

As an image

  1. Make a large box (array variable)
  2. Make a partition (array element) in the box
  3. Put a thing (value) in the partition

Such a place.

1. Declare variables in the array

To declare an array variable, write as follows.

Array element data type[]Variable name of array;

The data type of an array element is the data type of the element to be assigned in the array. An integer is int [], a decimal is double [], and a string is String [].

The variable name of the array is basically the English plural that represents the contents of the array. Scores if the contents of the array are the numbers of the test scores, colors if the string of the color name, etc.

Example: Define an array of integers numbers

//Since it is an integer, the data type is int, after the data type[]Define with
int[] numbers;

2. Create element

To assign an element, write as follows.

Variable name of array=Data type of new element[Number of elements];

Create an array element for the variable declared earlier.

The data type of the element is the same as the data type when the variable is declared. The number of elements is the number of values you want to put in the array.

Example: Assign three elements to the array numbers defined earlier

//Since numbers is an array of integers, the data type is int
//Since there are three elements, the number of elements is[3]
numbers = new int[3];

3. Assign value

To assign a value to an array, write as follows.

Variable name of array[index number] =Value you want to assign;

Example: Substitute the integers 10, 20, 30 for numbers

//Note that the index number starts from 0
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;

After creating the elements of the array, if the value is not entered, the value is automatically assigned according to the data type when compiling. (0 for int, null for String, etc.)

Describe array creation and value assignment together

If you create an array by following the above 3 steps, it will be as follows.

Example: Create an array of integers, assign numbers 10, 20, 30

//Declare a variable
int[] numbers;

//Create element
numbers = new int[3];

//Assign value
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;

However, these can also be described together as follows.

Array element data type[]Variable name of array= {Value 1,Value 2,Value 3, ...};
//Example: Make an array of integers numbers and integer 10, 20,Substitute 30
int[] numbers = {10, 20, 30};

Recommended Posts

How to make a Java array
How to make a Java container
How to make a Java calendar Summary
Java --How to make JTable
How to initialize Java array
How to make a JDBC driver
[Java] How to create a folder
How to make a splash screen
How to make a Jenkins plugin
How to convert a file to a byte array in Java
How to make a Maven project
Learning Ruby with AtCoder 13 How to make a two-dimensional array
How to add a new hash / array
[Introduction to Java] How to write a Java program
How to print a Java Word document
How to make shaded-jar
How to make a groundbreaking diamond using Java for statement wwww
How to display a web page in Java
I did Java to make (a == 1 && a == 2 && a == 3) always true
How to create pagination for a "kaminari" array
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
How to convert a solidity contract to a Java contract class
How to make a lightweight JRE for distribution
How to make a follow function in Rails
[Java] How to make multiple for loops single
[Java] How to turn a two-dimensional array with an extended for statement
[Java] [For beginners] How to insert elements directly in a 2D array
How to make an app with a plugin mechanism [C # and Java]
[Java] How to use Map
How to lower java version
How to uninstall Java 8 (Mac)
How to use java Optional
How to minimize Java images
How to write java comments
How to leave a comment
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to use string.format
How to use Java Map
[Java] Make it a constant
How to set Java constants
How to use Java variables
How to convert Java radix
[Java] Convert ArrayList to array
[Rails] How to make seed
[Java] How to implement multithreading
[Java] How to use Optional ①
How to insert a video
Make a rhombus using Java
How to create a method
How to make a factory with a model with polymorphic association
How to create a Java environment in just 3 seconds
How to use an array for a TreeMap key
How to jump from Eclipse Java to a SQL file
java: How to write a generic type list [Note]
[Java] How to play rock-paper-scissors (equivalent to paiza rank A)
How to make Java unit tests (JUnit & Mockito & PowerMock)
How to make JavaScript work on a specific page
How to create a data URI (base64) in Java