[Java ~ Array ~] Study memo 4

Learning Java. I will make a note so that I can look back on it for review. It's almost my study memo. Please do not excessive expectations.

Array

An array is like a set of variables. Arrays can store multiple values ​​, whereas variables can only handle one value. Each value in the array is called a element .

--Array variable definition

When dealing with arrays, assign the array to an array type variable. The array type must be specified as "array type with int type element" and "array type with String type element". Arrays with int type elements are int [] , An array with String type elements is described as String [] .

--How to make an array

An array can be created by writing values ​​in {}, separated by commas (,). Be careful not to confuse [] with {}. Note that a semicolon (;) is also required at the end of the array

How to make an array


//Array with int type elements
int[] numbers = {"3", "6", "9"};

//Array with elements of type String
String[] names = {"Aki", "John", "Bob"};

--Index number

The elements of the array are assigned numbers such as "0, 1, 2 ..." in order from the front. Note that this is called the index number and the index number starts at 0.

--Getting elements

Each element of the array can be obtained by using the array name [index number].

How to make an array


String[] names = {"Aki", "John", "Bob"};

//Output element with index number 2
System.out.println(names[2]);

Output result


Bob

--Overwrite array elements

It is possible to overwrite an element by assigning a value to a specific element.

How to make an array


String[] names = {"Aki", "John", "Bob"};

System.out.println(names[1]); //①
//Overwrite array elements
names[1] = "Taro";
System.out.println(names[1]); //②

Output result


① John

② Taro

Array and repeat

--Array and for statement

In order to output all the elements of the array, you can easily list them by using the for statement.

How to make an array


String[] names = {"Aki", "John", "Bob"};

for(int i=0; i<0; i+=1){
  System.out.println("my name is," + names[i] + "is."); //i is "0",1,Repeated only during "2"
}

Output result


My name is Aki.
My name is John.
My name is Bob.

Arrays have a feature called length that counts the number of elements. length is used by connecting with dots (.) Like " array.length ". By using length, the conditional expression "i <3" in the for statement can be rewritten. You don't have to worry about the number of elements in the array.

How to make an array


String[] names = {"Aki", "John", "Bob"};

for(int i=0; i<names.length; i+=1){
  System.out.println("my name is," + names[i] + "is.");
}

--For statement for array

The for statement has a special syntax (extended for statement) for arrays. If you use this, you can write the for statement more simply.

How to write


for(Data type variable name:Array name){
  //Iterative processing content;
}

How to make an array


String[] names = {"Aki", "John", "Bob"};

for(String name: names){
  System.out.println("my name is," + name + "is.");
}

Output result


My name is Aki.
My name is John.
My name is Bob.


Past posts

[Java ~ Variable definition, type conversion ~] Study memo [Java ~ False Value ~] Study Memo 2 [Java ~ Conditional branching / Iterative processing ~] Study memo 3

Recommended Posts

[Java ~ Array ~] Study memo 4
[Java ~ Method ~] Study memo (5)
Java array
Java array
java (array)
Java array
Java Silver Study Method Memo
[Java] Array
[Java ~ Boolean value ~] Study memo (2)
Java memo
Java array
Java study memo 2 with Progate
java array
[Java] Array
Java learning memo (creating an array)
java anything memo
java array variable
Let's study Java
Java Silver memo
java, maven memo
[Java] Array notes
Java SE 7 memo
java anything memo 2
[Java] Study notes
Java 8 study (repeatable)
Java study memorandum
Study Java Silver 1
Java specification memo
Java pattern memo
[Java ~ Variable definition, type conversion ~] Study memo
[Java ~ Conditional branching / Iterative processing ~] Study memo (3)
[Java ~ Classes and External Libraries ~] Study Memo (6)
Java development environment memo
Java Silver Study Day 1
java basic knowledge memo
Java learning memo (method)
Java Kuche Day memo
Study session memo: Kansai Java Engineers Association 8/5 --Selenium
About Java Array List
Dot installation study memo 01
Java study # 1 (typical type)
Java learning memo (basic)
java lambda expression memo
(Memo) Java for statement
My Study Note (Java)
Java lambda expression [memo]
Java learning memo (interface)
Two-dimensional array by just starting to study Java
[Java] Implicit inheritance memo
Java learning memo (inheritance)
java competitive programming memo
[Memo] Java Linked List
Study Java # 2 (\ mark and operator)
Java (WebSphere Application Server) memo [1]
Java memo (standard class) substring
[Java] List type / Array type conversion
Play Framework Study Memo Database ①
Study java arrays, lists, maps
Ruby study memo (conditional branching)
Java memo (standard class) length
Create a java method [Memo] [java11]