I want to compare the arrays

Asked 2 years ago, Updated 2 years ago, 93 views

You want to compare an array with multiple arrays For example, A={1,1,1,1}, B={{0,0,0,00},{1,1,1,1},{2,2,2,2},{3,3,3,3},...}, C={10,10,10},{20,20,20},...} ,D = {},... I want to compare A and B, compare A and C, compare A and D, and compare Z again

if (Arrays.equals(A, B[1]) & Arrays.equals(A, C) & Arrays.equals(A, D) &.............) Do you want me to do it like this?

array exception

2022-09-22 11:38

1 Answers

If you look at the previous questions, it's all Java questions, so suppose it's Java.

equals cannot compare whether the elements are all the same. You must use the deepEquals method.

jshell> int[] A={1,1,1,1}
A ==> int[4] { 1, 1, 1, 1 }

jshell> int[] B={1,1,1,1}
B ==> int[4] { 1, 1, 1, 1 }

jshell> int[] C={1,1,1,1}
C ==> int[4] { 1, 1, 1, 1 }

jshell> Object[] a_o = {A}
a_o ==> Object[1] { int[4] { 1, 1, 1, 1 } }

jshell> Object[] b_o = {B}
b_o ==> Object[1] { int[4] { 1, 1, 1, 1 } }

jshell> Object[] c_o = {C}
c_o ==> Object[1] { int[4] { 1, 1, 1, 1 } }

jshell> import java.util.Arrays;

jshell> Arrays.deepEquals(a_o, b_o) && Arrays.deepEquals(a_o, c_o)
$9 ==> true


2022-09-22 11:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.