-6

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.

4

1 Answer 1

0

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;}
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Your welcome, Please don't forget to select the correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.