I tried using an extended for statement in Java

I wasn't used to using extended for statements to implement iterative processing in Java, so I decided to review the simple usage this time. It is a level that I hope that beginner programmers will find it useful in terms of content.

Premise

Please note that the extended for statement is a function of Java SE 5.0 or later, so it is not possible if you are using Java earlier than that.

Basic usage

What can be achieved with an extended for statement

--You can take out the elements of an array or collection one by one and execute processing on each element. --The description can be simplified

First of all, how to use the conventional for statement is as follows

Traditional notation


for(Equation 1;Equation 2;Equation 3){

processing;

}

And the extended for statement is as follows

Extended for statement


for(Variable declaration:Reference variable name){ //In other words, in parentheses(Type variable name:formula)

processing;

}

Use the extended for statement to process all elements of the array

Immediately write the process

sample.java


public static void main (String args[]) {
		//Finally put the elements in the array
		List<String> list = new ArrayList<>();
		list.add("Apple");
		list.add("Gorilla");
		list.add("Rappa");
		list.add("Pineapple");
		list.add("rumba");

		//From here is the production

		//Extracts all elements of the variable name list one by one and outputs them as standard output.
		for(String word : list) { // for(Type variable name:formula)
			System.out.println(word);
		}
		
	}

The output result is as follows

result


Apple
Gorilla
Rappa
Pineapple
rumba

Other

――This example is simple, but if you can master it, it will be easier to describe the iterative process!

Recommended Posts

I tried using an extended for statement in Java
I tried using JWT in Java
I tried using Elasticsearch API in Java
[Java] for statement / extended for statement
[For beginners] I tried using DBUnit in Eclipse
[For beginners] I tried using JUnit 5 in Eclipse
I tried using Java REPL
I tried metaprogramming in Java
I sent an email in Java
I made an annotation in Java.
I tried using Java memo LocalDate
I tried using GoogleHttpClient of Java
[Java] Make variables in extended for statement and for Each statement immutable
[Java] Get Map key / value pairs using extended for statement
I tried using Dapr in Java to facilitate microservice development
I tried Cassandra's Object Mapper for Java
Map without using an array in java
I tried the new era in Java
I tried using OpenCV with Java + Tomcat
Java for statement
[Java] How to turn a two-dimensional array with an extended for statement
I tried to make a talk application in Java using AI "A3RT"
A story that I realized that I had to study as an engineer in the first place
I tried using an extended for statement in Java
I tried using Docker for the first time
ChatWork4j for using the ChatWork API in Java
I want to send an email in Java.
I tried to implement deep learning in Java
I tried to output multiplication table in Java
I tried to develop an application in 2 languages
I tried to create Alexa skill in Java
[Java] for statement, while statement
I tried using Gson
I tried using TestNG
I tried using Galasa
(Memo) Java for statement
[Java] How to get HashMap elements by loop control using extended for statement
I tried using a database connection in Android development
I tried Mastodon's Toot and Streaming API in Java
I tried to implement Firebase push notification in Java
I wrote about Java downcast in an easy-to-understand manner
I tried to operate SQS using AWS Java SDK
# 2 [Note] I tried to calculate multiplication tables in Java.
I tried using the Migration Toolkit for Application Binaries
I tried to create a Clova skill in Java
I tried to make a login function in Java
I tried using Log4j2 on a Java EE server
What I learned in Java (Part 3) Instruction execution statement
I tried using Docker Desktop for Windows on Windows 10 Home
[Beginner] Java variable / logical operator / extended for statement [Note 22]
I tried passing Java Silver in 2 weeks without knowing Java
How to loop Java Map (for Each / extended for statement)
I tried to implement the Euclidean algorithm in Java
~ I tried to learn functional programming in Java now ~
I tried scraping a stock chart using Java (Jsoup)
A memorandum for creating an extended logger using org.slf4j.Logger
I tried to find out what changed in Java 9
I tried to build an environment using Docker (beginner)
Try using RocksDB in Java
[Java] Basic statement for beginners
I made roulette in Java.
I tried Drools (Java, InputStream)