I want to compare two arrays of a custom class, which stores some Strings and shorts.The aim is, to get those Items from the second array, which aren't in the first array. As a return I want an array of my class.
thanks for your help.
I want to compare two arrays of a custom class, which stores some Strings and shorts.The aim is, to get those Items from the second array, which aren't in the first array. As a return I want an array of my class.
thanks for your help.
First you would need to make a method in your custom class that accepts an object of the same class and tests whether the imputed object has the same values as the object which is calling the method. Then just loop through the array and see if the object is equal to another object. This has already been done in the String class. In the String class there is a method
equals(String s)
which you could use
Here is an example of how you would compare 2 String arrays. To make it work with you custom class, just change the array type and make a .equals method in your class.
String[] array1={"Hello","Hi"};
String[] array2={"Hello","eat"};
for(int x=0;x<array1.length;x++){
Boolean present=false;
for(int y=0;y<array2.length;y++){
if(array1[x].equals(array2[y])){
present=true;
break;
}
}
if(!present){
System.out.println(array1[x]);
}
}
EDIT:
here is how you would make your equals method in your custom class
public class custom{
int value;
String text;
public Boolean equals(custom obj){
if(this.value==obj.value && this.text.equals(obj.text)){
return true;
}else{return false;}
}
}