Click here to Skip to main content
15,913,181 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear,

i have two arrays and need to compare index 1 of one array and index 1 of second array like 2-2,3-3 like that upto array length at any time mismatch means exit

C#
String tt="Adyar, Chennai, Tamil Nadu, India";
        String[] parts = tt.split(",");
        String tt1="Chennai, Tamil Nadu, India";
        String[] parts1 = tt1.split(",");

need compare this two array form last

if sencond array is compare sucessfully with first array then need give output

thanks in advance
Posted
Updated 7-Jan-14 1:03am
v2
Comments
BK 4 code 7-Jan-14 7:03am    
exactly what do you need as a result after comparison?
phil.o 7-Jan-14 7:12am    

Java
boolean comArr(String [] sa1, String [] sa2)
{
  if (sa1.length != sa2.length) return false;
  for (int n=0; n<sa1.length; ++n)
  {
    if ( ! sa1[n].equals(sa2[n]) ) return false;
  }
  return true;
}


[update]
In order to check if an array contains the other, you might use:
(Please note you have to use split(", "), in your original code)
Java
public static boolean comArr(String [] sa1, String [] sa2)
{
  String [] bigger;
  String [] smaller;

  if (sa1.length < sa2.length)
  {
    bigger = sa2;
    smaller = sa1;
  }
  else
  {
    bigger = sa1;
    smaller = sa2;
  }
  int d = bigger.length - smaller.length;

  for (int offset = 0; offset <= d; ++offset)
  {
    int n;
    for (n=0; n<smaller.length; ++n)
    {
      if ( !smaller[n].equals(bigger[offset+n])) break;
    }
    if ( n == smaller.length) return true;
  }

  return false;
}

[/update]
 
Share this answer
 
v3
Comments
mekalareddy 7-Jan-14 7:57am    
thanks pallini
but size of array are not same in some conditions
CPallini 7-Jan-14 8:02am    
If the size is not the same how can they be equal?
Are you instead checking if one of the arrays contains the other?
mekalareddy 7-Jan-14 8:25am    
ya pallini sir,but if 2 array elements all are match with first array means enough
for example
String tt="aa,aaa,aaaa,Chennai, Tamil Nadu, India";
String[] parts = tt.split(",");
String tt1="aaaa,Chennai, Tamil Nadu,India";
String[] parts1 = tt1.split(",");
like this two arrays if 2 array all element in first with corresponding index then return true
CPallini 7-Jan-14 8:34am    
That confirms you are checking if an array contains (or is equal to) the other one.
mekalareddy 7-Jan-14 8:43am    
need to check if there 10 elements in first and 5 element in second array check like 10-5,9-4,8-3,7-2,6-1 like that based on second array no need of size if match return true
You can also use Arrays.deepEquals:

Java
boolean isEqual = Arrays.deepEquals(parts, parts1);
 
Share this answer
 
Comments
mekalareddy 7-Jan-14 8:29am    
thanks Giannakakis Kostas, but if different length means

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900