[Java] How to turn a two-dimensional array with an extended for statement

Extended for statement

for(Type variable name: array){
}

I was wondering for a moment how to expand a two-dimensional array by combining the method of turning a loop while assigning the array on the right of: to the variable on the left, like PHP.

foreach($datas as $key => $value){
 for($i = 0; $i < count($value); i++){
    $value[i] = 0;
  }
}

I thought of using foreach, but it seems that I have to create another object in java, so I wanted to do something about it.

A story that you only have to write the expansion for twice

public class Control {

	public static void main(String[] args) {
		int[][] datas = {
				{0,0,0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,1,1,1,1,1,0},
				{0,0,0,0,0,0,1,1,1,1,0},
				{0,0,0,0,0,1,1,1,1,1,0},
				{0,0,0,0,1,1,1,1,1,1,0},
				{0,0,0,1,1,1,1,1,0,1,0},
				{0,0,1,1,1,1,1,0,0,0,0},
				{0,0,0,1,1,1,0,0,0,0,0},
				{0,0,0,0,1,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0,0,0},
		};
		for(int[] data : datas ) {
			for(int value : data) {
				if(value == 0) {
					System.out.print("  ");
				} else {
					System.out.print("* ");
				}
			}
			System.out.println(""); //Line breaks after expanding the nested array
		}
		
				
	}
}

Execution result


                      
          * * * * *   
            * * * *   
          * * * * *   
        * * * * * *   
      * * * * *   *   
    * * * * *         
      * * *           
        *             
                      

It wasn't something that bothered me, but I felt a little inconvenient that foreach couldn't use it without creating another object, and I was a little surprised when I thought, "Do I have to remember that too?" I will leave it as.

Recommended Posts

[Java] How to turn a two-dimensional array with an extended for statement
How to use an array for a TreeMap key
[Java] How to search for a value in an array (or list) with the contains method
How to loop Java Map (for Each / extended for statement)
How to make a Java array
How to divide a two-dimensional array into four with ruby
Learning Ruby with AtCoder 13 How to make a two-dimensional array
How to output array values without using a for statement
How to make a groundbreaking diamond using Java for statement wwww
[Java] How to test for null with JUnit
How to use an array for HashMap keys
How to create pagination for a "kaminari" array
I want to ForEach an array with a Lambda expression in Java
[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 convert one element of a String type array to an Int type
How to deploy a system created with Java (Wicket-Spring boot) to an on-campus server
[Java] How to get HashMap elements by loop control using extended for statement
How to make a judgment method to search for an arbitrary character in an array
[Java basics] Let's make a triangle with a for statement
How to output standard from an array with forEach
I tried using an extended for statement in Java
How to convert a file to a byte array in Java
[Java] How to start a new line with StringBuilder
How to initialize Java array
(Java) How to implement equals () for a class with value elements added by inheritance
Store in Java 2D map and turn with for statement
How to create a lightweight container image for Java apps
How to change a string in an array to a number in Ruby
How to test a private method with RSpec for yourself
For Java beginners: List, Map, Iterator / Array ... How to convert?
Investigated how to call services with Watson SDK for Java
How to make a Java container
[Java] How to create a folder
How to turn Iterator Dojo (Java)
Try to imitate the idea of a two-dimensional array with a one-dimensional array
Sorting a list with an int array as an element (Java) (Comparator)
Cast an array of Strings to a List of Integers in Java
How to check for the contents of a java fixed-length string
How to deal with the type that I thought about writing a Java program for 2 years
How to crop an image with libGDX
How to make a Java calendar Summary
How to compile Java with VsCode & Ant
[Java] How to compare with equals method
How to add a new hash / array
[Introduction to Java] How to write a Java program
How to create a Maven repository for 2020
How to make a Discord bot (Java)
How to print a Java Word document
[Personal memo] How to interact with a random number generator in Java
How to convert an array of Strings to an array of objects with the Stream API
How to create a database for H2 Database anywhere
Create a CSR with extended information in Java
How to display a web page in Java
How to use Java framework with AWS Lambda! ??
Convert a string to a character-by-character array with swift
How to turn off ChromeDriver notification bar (Java)
How to convert a solidity contract to a Java contract class
Array vs ArrayList vs HashMap Extended for Statement Race
How to use Java API with lambda expression
How to make a lightweight JRE for distribution