Results 1 to 3 of 3
- 02-17-2011, 01:54 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 24
- Rep Power
- 0
comparing 2 arrays to see if they're equal
I have to write a program that will create 2 boolean arrays and compare them to see if they are equal. The program contains a class that has all the methods I can use on the arrays and a another class with a main that tests those methods. The only indexes that I'm using in the array are the ones that contain true values, the other are not important. I'm having a problem with the method called equals. It is supposed to check if the two arrays are equal and if they are it will return true. When I use the method I keep getting false even though I know that the two arrays are copies of each other, because I used another method called copy to create the second array. I cant seem to find where I am going wrong and would really appreciate some help.
Thanks
Java Code:public class Intcoll3 //This is the class that contains all of my methods the method I'm having trouble with is at the bottom and called equals { private boolean[] c; private int how_many; public Intcoll3() { c = new boolean[500]; how_many = 0; } public Intcoll3(int i) { c = new boolean[i]; how_many = 0; } public void copy(Intcoll3 obj) { if (this != obj) { c = new boolean[obj.c.length]; this.how_many = obj.how_many; for(int j = 0; j < obj.c.length; j++) { c[j] = obj.c[j]; } //this.how_many = obj.how_many; } } public boolean belongs(int i) { boolean belongs; if((i >= 0) && (i < c.length)) { belongs = (c[i] == true); } else { belongs = false; } return (belongs); } public void insert(int i) { if (i >= 0) { if(i < c.length) { if(c[i] != true) { c[i] = true; how_many++; } } else { boolean[] d = new boolean[i * 2]; for(int k = 0; k < c.length; k++) { d[k] = c[k]; } c = d; if(c[i] != true) { c[i] = true; how_many++; } } } } public void omit(int i) { if(i < c.length) { if (c[i] == true) { c[i] = false; how_many --; } } else { System.out.println("The number you entered is to large"); System.exit(1); } } public int get_howmany() { return how_many; } public void print() { int j = 0; while (j < c.length) { if(c[j] == true) { System.out.println(j); } j++; } } public boolean equals(Intcoll3 obj)//This is the method I'm having trouble with I want to use the belongs method to see if the index contains a true value { int j = 0; boolean result = true;//Also j refers to the index's that contain the true values. I only care about the indexes with true the other are not important if(this.how_many != obj.how_many) { result = false; } else { while ((j <= obj.c.length)&&result) { result = obj.belongs(j); j++; } } return result; } }Java Code:import java.util.Scanner; public class Intcoll3client //This class has a main program that tests my methods { public static final int SENTINEL = -1; public static void main(String[] args) { int value; int test; Scanner kb = new Scanner(System.in); Intcoll3 X = new Intcoll3(); Intcoll3 Y = new Intcoll3(); System.out.println("Enter the index where you would like to insert the true value or -1 to quit: "); value = kb.nextInt(); while(value != SENTINEL) { X.insert(value); System.out.println("Enter the index where you would like to insert the True value or -1 to quit: "); value = kb.nextInt(); } System.out.println("The following indexes have true values"); X.print(); Y.copy(X); //This makes Y a copy of X System.out.println("Y is a copy of X and contains: "); Y.print(); System.out.println("The equals method for X and Y returned: " + Y.equals(X)); //This is where I call the equals method that isn't working } }
- 02-17-2011, 06:09 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
:DJava Code:public boolean equals(Intcoll3 obj)//This is the method I'm having trouble with I want to use the belongs method to see if the index contains a true value { return Arrays.equals(this.c, obj.c); }
Your code is very complicated. Iterate from 0 to array.length-1 and check if this.c[i] != obj.c[i]. If this is true, return false immediately, if not, iterate further. if your for-loop ends, you can return true!
- 02-17-2011, 06:59 AM #3
Member
- Join Date
- Feb 2011
- Location
- Ahmedabad
- Posts
- 36
- Rep Power
- 0
Similar Threads
-
Comparing Arrays in Java
By Daykeem in forum New To JavaReplies: 6Last Post: 01-15-2011, 04:25 AM -
Comparing arrays
By mitty in forum New To JavaReplies: 8Last Post: 04-14-2010, 11:55 AM -
equal() method
By need_helpp in forum New To JavaReplies: 3Last Post: 03-09-2010, 05:57 PM -
Comparing two Char arrays
By viperlasson in forum New To JavaReplies: 3Last Post: 01-30-2010, 08:05 AM -
comparing arrays..
By circuspeanuts in forum New To JavaReplies: 5Last Post: 05-25-2009, 07:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks