java - Comparing two elements of a string array with .equals() -
i'm working on project , need compare 2 elements of same array
private void checkimports() { //import tester for(int = 0; i<theclass.length;i++) { for(int j = 0; j<i; j++) { if(theclass[i].equals(theclass[j])) { system.out.println("there double import @ line " +i); adderror("double import @ line ",i) } } } }
the array filled following strings:
"import java.x;" //0 "import java.y;" //1 "import java.z;" //2 "import java.x;" //3
for reason when call equals() method never evaluates true when = 3 , j= 0. explanation why adderror method never called appreciated
there 2 possible explanations (i mean, there many explanations, these 2 lend checking first, think):
- the comparison
i==3
,j==0
never performed (even though think is). - the values
theclass[0]
,theclass[3]
not equal (even though think are).
you can check both cases, e.g., of debugger. if don't using debugger, can insert few system.out.println
statements check values variables i
, j
assume , theclass[0].equals(theclass[3])
evaluates true
.
Comments
Post a Comment