I want to simplify the conditional if-else statement in Java

Introduction

It will be first post. The author is an amateur who started programming in April. This article was written as a memorandum, but I would appreciate it if you could tell me strictly!

I tried using the abbreviation of the if-else statement!

I ended up writing this kind of code when I was training.

samp0.java


import java.util.Arrays;

public class MyJava {
	public static void main(String[] args) {
		int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		int[] b = new int[a.length];

		for (int i = 0; i < a.length; i++) {
			//Evenness judgment of the i-th element of a
			if (a[i] % 2 == 0) {
				b[i] = 0;
			} else {
				b[i] = 1;
			}
		}
       //Although it deviates from the main subject, it is convenient to remember the printf method
       // Arrays.toString(array)Is convenient because you don't have to turn for when you want to output an array.
		System.out.printf("The contents of the b array%s.\n",
									Arrays.toString(b));
	}
}

if-else statement simplification ver, 1

It's unpleasant that the layer gets deeper with this kind of processing ... When I was looking into it like that ...

that? If this is in the if statement, you should continue, right? else sentence will decrease! So here's the code:

samp1.java


import java.util.Arrays;

public class MyJava {
	public static void main(String[] args) {
		int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		int[] b = new int[a.length];

		for (int i = 0; i < a.length; i++) {
			//Evenness judgment of the i-th element of a
			//Substitute 0 for even numbers and 1 for odd numbers
			if (a[i] % 2 == 0) {
				b[i] = 0;
				continue;
			}
	        b[i] = 1;
		}

		System.out.printf("The contents of the b array%s.\n",
									Arrays.toString(b));
	}
}

if-else statement simplification ver, 2

I was able to do it, but this time the judgment when it is an odd number is difficult to read ...

Hmm! ?? this is,,,! ??

Simplified if-else statement

hoge ? fuga : foo

What is this?

When hoge is True, fuga is executed, and when false, foo is executed.

I see! You don't have to make strange layers! Alright!

samp2.java


import java.util.Arrays;
public class MyJava {
	public static void main(String[] args) {
		int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		int[] b = new int[a.length];

		for (int i = 0; i < a.length; i++) {
			b[i] = a[i] % 2 == 0 ? 0 : 1;
		}

		System.out.printf("The contents of the b array%s.\n",
									Arrays.toString(b));
	}
}

Hmm, clean!

Afterword

It's my first post, so I'm sorry for the poor text. I'd like to keep a record of what I've learned little by little, so I'd appreciate it if you could say, "It's easier to read!" Thank you for reading this far.

Reference site

Java way assignment operators and other operators (http://www.javaroad.jp/java_operator6.htm)

Recommended Posts

I want to simplify the conditional if-else statement in Java
[Java] I want to perform distinct with the key in the object
I want to send an email in Java.
rsync4j --I want to touch rsync in Java.
I want to get the value in Ruby
I want to get the IP address when connecting to Wi-Fi in Java
I want to do something like "cls" in Java
[Java] I want to calculate the difference from the date
I want to embed any TraceId in the log
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I tried to implement the Euclidean algorithm in Java
I want to simplify the log output on Android
Conditional branching of the result of SQL statement to search only one in Java
I want to set the conditions to be displayed in collection_check_boxes
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to transition to the same screen in the saved state
# 1_JAVA I want to get the index number by specifying one character in the character string.
In Java, I want to trim multiple specified characters from only the beginning and end.
I want to return to the previous screen with kotlin and java!
I want to display the images under assets/images in the production environment
I want to change the value of Attribute in Selenium of Ruby
[Android] I want to get the listener from the button in ListView
Java reference to understand in the figure
How to get the date in java
I want to stop Java updates altogether
I want to use @Autowired in Servlet
I went to the Java Women's Club # 1
[Introduction to Java] Conditional branching (if statement, if-else statement, else if statement, ternary operator, switch statement)
I want to write JSP in Emacs more easily than the default.
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
(Limited to Java 7 or later) I want you to compare objects in Objects.equals
I called the COTOHA API parser 100 times in Java to measure performance.
I want to display an error message when registering in the database
I want to ForEach an array with a Lambda expression in Java
I want to output the day of the week
I tried to organize the session in Rails
I want to use arrow notation in Ruby
I tried to implement deep learning in Java
I want to use java8 forEach with index
I want to var_dump the contents of the intent
I want to pass APP_HOME to logback in Gradle
[Xcode] I want to manage images in folders
I want to write quickly from java to sqlite
I want to truncate after the decimal point
I tried to create Alexa skill in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
I want to play a GIF image on the Andorid app (Java, Kotlin)
I want you to use Scala as Better Java for the time being
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
I was addicted to the NoSuchMethodError in Cloud Endpoints
I want to use Clojure's convenient functions in Kotlin
[Java] How to omit the private constructor in Lombok
I tried to organize the cases used in programming
Tokoro I rewrote in the migration from Wicket 7 to 8
I tried to implement Firebase push notification in Java
I want to use fish shell in Laradock too! !!
# 2 [Note] I tried to calculate multiplication tables in Java.
I want to judge the range using the monthly degree
Source used to get the redirect source URL in Java
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment