-6

I need to compare rm.rel object array with agent array and also object array .i dont know how to perform it ? whether i need to store agent and object as some thing like arraylist or hashmap..

here is my code... The rm.rel array consist of values like (mod pur and agt agt and obj obj mod mod mod man agt cob mod obj obj agt man cnt agt)

public String[] agent={"agt","cag","aoj","cao","ptn"};
public String[] object={ "obj", "cob", "opl", "ben"};
public void compare()

  {
      temp=new String[rm.length]; 
      for(int k=0;k<rm.length;k++)
      {
                   temp[k]=rm[k].rel[k];

      }
      for(int i=0;i<rm.length;i++)
      {
       for(int j=0;j<agent.length+object .length;j++)
          {
          if(temp[i].equals(agent[j]))
          {
              temp[i]="agent";
          }
          else if(temp[i].equals(object[j])) // It shows arrayout of bound exception
          {
              temp[i]=rm[i].rel[i];
          }
      }
      }
      for(int k=0;k<temp.length;k++)
      {
          System.out.println(temp[k]);
      }
  }

i need to compare each element of rm.rel array with all elements of agent array if it is not equal then it should compare with object array pls help

user2942551
  • 1
  • 1
  • 3

3 Answers3

2

Without knowing what you're actually trying to compare for, I can tell you why you're geting ArrayIndexOutOfBoudsException. Your problem is here

for(int j = 0; j < agent.length + object.length; j++)

j will increment up to the size of both arrays combined, but max index for object is 3. Same with the agent array. j cannot be accesed passed 5.

Currently you have the loop looping up to 4 + 5 (9).

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
1

Try this because your compare method is void. You just try to see if they are equal or not.

Arrays.sort(agent);
Arrays.sort(object)
boolean isEql = Arrays.equals(agent, object);

if array are equal it will return true otherwise false.

According to Java doc:

Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order

  1. Arrays.equals Returns true if the two specified arrays of Objects are equal to one another. The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. Two objects e1 and e2 are considered equal if (e1==null ? e2==null : e1.equals(e2)). In other words, the two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.
  2. Arrays.deepEquals Returns true if the two specified arrays are deeply equal to one another. Unlike the equals(Object[],Object[]) method, this method is appropriate for use with nested arrays of arbitrary depth.

How deepEquals work see this example by @polygenelubricants?

Community
  • 1
  • 1
Prateek
  • 6,785
  • 2
  • 24
  • 37
1
if(temp[i].equals(object[j])) // It shows arrayout of bound exception

Don't want personally to be "Captain Obvious", but it's a good idea to check an index vs the array length before trying to access array item by index. For example:

if((object.length > j) && (temp[i].equals(object[j])))

And yes, use "Array.equals()" to compare arrays, as @Prateek proposed before.

Yury Schkatula
  • 5,291
  • 2
  • 18
  • 42