OnJava8-Examples/arrays/ComparingArrays.java

63 lines
2.0 KiB
Java
Raw Permalink Normal View History

2015-09-07 11:44:36 -06:00
// arrays/ComparingArrays.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2015-06-15 17:47:35 -07:00
// Using Arrays.equals()
import java.util.*;
2016-01-25 18:05:55 -08:00
import onjava.*;
2015-06-15 17:47:35 -07:00
public class ComparingArrays {
2016-01-25 18:05:55 -08:00
public static final int SZ = 15;
static String[][] twoDArray() {
2016-01-25 18:05:55 -08:00
String[][] md = new String[5][];
Arrays.setAll(md, n -> new String[n]);
for(int i = 0; i < md.length; i++)
Arrays.setAll(md[i], new Rand.String()::get);
return md;
}
2015-06-15 17:47:35 -07:00
public static void main(String[] args) {
2016-01-25 18:05:55 -08:00
int[] a1 = new int[SZ], a2 = new int[SZ];
Arrays.setAll(a1, new Count.Integer()::get);
Arrays.setAll(a2, new Count.Integer()::get);
System.out.println(
"a1 == a2: " + Arrays.equals(a1, a2));
2015-06-15 17:47:35 -07:00
a2[3] = 11;
2016-01-25 18:05:55 -08:00
System.out.println(
"a1 == a2: " + Arrays.equals(a1, a2));
Integer[] a1w = new Integer[SZ],
a2w = new Integer[SZ];
Arrays.setAll(a1w, new Count.Integer()::get);
Arrays.setAll(a2w, new Count.Integer()::get);
System.out.println(
"a1w == a2w: " + Arrays.equals(a1w, a2w));
a2w[3] = 11;
System.out.println(
"a1w == a2w: " + Arrays.equals(a1w, a2w));
String[][] md1 = twoDArray(), md2 = twoDArray();
2016-01-25 18:05:55 -08:00
System.out.println(Arrays.deepToString(md1));
System.out.println("deepEquals(md1, md2): " +
Arrays.deepEquals(md1, md2));
System.out.println(
"md1 == md2: " + Arrays.equals(md1, md2));
md1[4][1] = "#$#$#$#";
System.out.println(Arrays.deepToString(md1));
System.out.println("deepEquals(md1, md2): " +
Arrays.deepEquals(md1, md2));
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2016-01-25 18:05:55 -08:00
a1 == a2: true
a1 == a2: false
a1w == a2w: true
a1w == a2w: false
2016-07-22 14:45:35 -06:00
[[], [btpenpc], [btpenpc, cuxszgv], [btpenpc, cuxszgv,
gmeinne], [btpenpc, cuxszgv, gmeinne, eloztdv]]
2016-01-25 18:05:55 -08:00
deepEquals(md1, md2): true
md1 == md2: false
2016-07-22 14:45:35 -06:00
[[], [btpenpc], [btpenpc, cuxszgv], [btpenpc, cuxszgv,
gmeinne], [btpenpc, #$#$#$#, gmeinne, eloztdv]]
2016-01-25 18:05:55 -08:00
deepEquals(md1, md2): false
2015-09-07 11:44:36 -06:00
*/