Compare Java arrays -
i'm trying compare names of roomtypes in simple java program can't work.
so far have this:
public class main { public static void main(string[] args) { roomtype[] ar = new roomtype[10]; string s = ""; string duplicate = ""; ar[0] = new roomtype("standaard", 2, 60.0); ar[1] = new roomtype("deluxe", 2, 85.0); ar[2] = new roomtype("deluxe", 4, 125.0); ar[3] = new roomtype("hiker", 2, 35.0); (int = 0; < ar.length; i++) { if (ar[i] != null) { s += ar[i] + "\n"; } (int j = 0; j < ar.length -1; j++) { if (ar[i] == ar[j] && ar[i] != null && ar[j] != null) { duplicate = ar[i] + " has same name " + ar[j]; } } } system.out.println("these roomtypes: \n" + s + "\n"); system.out.println(duplicate); }
}
i want compare names (the first elements in arrays) of ar[]
's , if there doubles need sysout gives position (ar[]
) of doubles. method gettypename() in different class roomtype.
here interpreted asking for:
for(int = 0; < ar.length; i++) { if(ar[i] != null) s += ar[i].gettypename()+"\n"; for(int j = 1; j < ar.length; j++) { if(ar[i] != null && ar[j] != null && ar[i].gettypename().equals(ar[j].gettypename())) { duplicate += i+" has same name "+j+"\n"; } } } system.out.println("these roomtypes: \n"+s+"\n"); system.out.println(duplicate);
Comments
Post a Comment