[JAVA] I got stuck in a clone of a two-dimensional array

If it is a one-dimensional array of ordinary primitive type, you can copy it with clone (). If it is an array of classes, you can make a shallow copy by cloning (). Then, if it is a primitive type two-dimensional array ...

clone () of int type 2D array

I used to make another two-dimensional array and copy the values, but when I saw someone's source, I thought I could write it in one line.

	public static void main(String[] args) {
		int[][] map = new int[3][3];
		for (int y=0; y<3; y++) {
			for (int x=0; x<3; x++) {
				map[y][x] = x+y;
			}
		}
		System.out.println("before");
		for (int y=0; y<3; y++) {
			for (int x=0; x<3; x++) {
				System.out.print(map[y][x] + " ");;
			}
			System.out.println("");
		}
		int[][] nmap = map.clone();
		for (int y=0; y<3; y++) {
			for (int x=0; x<3; x++) {
				nmap[y][x] = 4-(x+y);
			}
		}
		System.out.println("after");
		for (int y=0; y<3; y++) {
			for (int x=0; x<3; x++) {
				System.out.print(map[y][x] + " ");;
			}
			System.out.println("");
		}
	}

When you run

before
0 1 2 
1 2 3 
2 3 4 
after
4 3 2 
3 2 1 
2 1 0 

I intended to make clone () in nmap, but map has been rewritten.

Array hash value

Try to output the hash value of int [].

	public static void main(String[] args) {
		int[][] map = new int[3][3];
		System.out.println("before");
		for (int y=0; y<3; y++) {
			System.out.println(map[y]);
		}
		int[][] nmap = map.clone();
		System.out.println("after");
		for (int y=0; y<3; y++) {
			System.out.println(nmap[y]);
		}
	}

When you run

before
[I@15db9742
[I@6d06d69c
[I@7852e922
after
[I@15db9742
[I@6d06d69c
[I@7852e922

It's the same. If you think about it carefully, it's natural, but since int [] is a reference type, it is a shallow copy.

Loop and clone () a one-dimensional array

Then clone () int [].

	public static void main(String[] args) {
		int[][] map = new int[3][3];
		System.out.println("before");
		for (int y=0; y<3; y++) {
			System.out.println(map[y]);
		}
		int[][] nmap = new int[3][];
		for (int y=0; y<3; y++) {
			nmap[y] = map[y].clone();
		}
		System.out.println("after");
		for (int y=0; y<3; y++) {
			System.out.println(nmap[y]);
		}
	}

When you run

before
[I@15db9742
[I@6d06d69c
[I@7852e922
after
[I@4e25154f
[I@70dea4e
[I@5c647e05

This time it's good.

Try cloning () the first source as a one-dimensional array

Try clone () with int [].

	public static void main(String[] args) {
		int[][] map = new int[3][3];
		for (int y=0; y<3; y++) {
			for (int x=0; x<3; x++) {
				map[y][x] = x+y;
			}
		}
		System.out.println("before");
		for (int y=0; y<3; y++) {
			for (int x=0; x<3; x++) {
				System.out.print(map[y][x] + " ");;
			}
			System.out.println("");
		}
		int[][] nmap = new int[3][];
		for (int y=0; y<3; y++) {
			nmap[y] = map[y].clone();
		}
		for (int y=0; y<3; y++) {
			for (int x=0; x<3; x++) {
				nmap[y][x] = 4-(x+y);
			}
		}
		System.out.println("after");
		for (int y=0; y<3; y++) {
			for (int x=0; x<3; x++) {
				System.out.print(map[y][x] + " ");;
			}
			System.out.println("");
		}
	}

When you run

before
0 1 2 
1 2 3 
2 3 4 
after
0 1 2 
1 2 3 
2 3 4 

nmap has a copy of the map.

Summary

Two-dimensional arrays cannot be clone () in one row. Even if you write it in someone's source, there is no guarantee that it is correct.

Recommended Posts

I got stuck in a clone of a two-dimensional array
I got stuck in File
I got stuck trying to write a where in clause in ActiveRecord
[JAVA] Project Euler, I got stuck in Q8, so make a note
Where I got stuck in today's "rails tutorial" (2020/10/08)
Stuck in an enum in front of a blacksmith
Where I got stuck in today's "rails tutorial" (2020/10/05)
Where I got stuck in today's "rails tutorial" (2020/10/06)
Where I got stuck in today's "rails tutorial" (2020/10/04)
Where I got stuck in today's "rails tutorial" (2020/10/07)
I got a cannot resolve symbol in Android Studio
[Ruby] Extracting a two-dimensional array
Multiplication in a Ruby array
I was confused because there was a split in the Array
I tried to make a client of RESAS-API in Java
When I switched to IntelliJ, I got a lot of differences in the encoding of the properties file.
[Personal memo] Looking back on what I got stuck in Docker ... including knowledge of Linux
Sorting hashes in a Ruby array
Try to imitate the idea of a two-dimensional array with a one-dimensional array
I created a PDF in Java.
[Ruby] I want to put an array in a variable. I want to convert to an array
Cast an array of Strings to a List of Integers in Java
I got a Permission Denied error when I put Laravel in Docker
Get stuck in a Java primer
I built a rails environment with docker and mysql, but I got stuck
I tried to make a parent class of a value object in Ruby
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
[Rails] I want to send data of different models in a form
I got stuck using snake case for variable name in Spring Boot
I want to ForEach an array with a Lambda expression in Java
[Java] I participated in ABC-188 of Atcorder.
Extract elements of array / extract in reverse order-java
I tried a calendar problem in Ruby
When seeking multiple in a Java array
What I got into @Transactional in Spring
I tried embedding a formula in Javadoc
Rails6 I want to make an array of values with a check box
12 of Array
I tried to clone a web application full of bugs with Spring Boot
I made a sample of how to write delegate in SwiftUI 2.0 using MapKit
I made a primality test program in Java
Measure the size of a folder in Java
I got stuck trying Volley's synchronization process (Java)
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I wrote a primality test program in Java
I made a rock-paper-scissors game in Java (CLI)
I tried to create a LINE clone app
I made a Ruby extension library in C
A quick review of Java learned in class
I wrote a prime factorization program in Java
A collection of RSpecs that I used frequently
I got stuck when port forwarding with VBox
Summary of what I learned in Spring Batch
Create a native extension of Ruby in Rust
How to find the total value, average value, etc. of a two-dimensional array (multidimensional array)-java
I hate this kind of code! A collection of anti-patterns actually seen in the field
A story that got stuck with an error during migration in docker PHP laravel
[Rails] I want to display the link destination of link_to in a separate tab