Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing two arrays elements

Status
Not open for further replies.

shonJava

Programmer
Dec 27, 2004
11
US
HI All,

I trying to build the logic for following requirement ..any help would be appreciated.
I have to compare the elements of array1 and array2.
I have to comapre the each element of array1 with the all elements of array2. If any of the element of array1 doen not exist in array2 then I have to run delete record in db.
Here is my code when run this code I get Index outof bound expection...

Code:
for (int k = 0; k < array1.length; k++){                    
  System.out.println("First For Loop");          
  String pID = array1[k].toStrin();                         
  System.out.println("pID>>>>>>>>" + pID);          
    for (int p = 0; p < array2.length; p++){                
       System.out.println("Second For Loop");            
       String cID = array2[p].toStrin();                    
       System.out.println("cID>>>>>>>>" + cID);            
       if (!pID.equals(cID)) {               
         //record deleted              
         System.out.println("Record Deleted");            
       }          
    }         
  }      
}
 
Some hints:

1.- You should post this in the Java forum (this is just for J2EE)

2.- You should post your actual code, I guess that one doesn't even compile

3.- If the second element in array one is the same as the first in array two, you will compare the two first ones. They're not equal but the element is deleted anyway.

Cheers,
Dian
 
I have to ask you this - last month you posted a question in the standard (J2SE) forum : forum269 - so you know that a standard Java forum exists.

But you have still posted a standard Java question in the J2EE forum.

Why ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Dammit, what I wrote is unreadable:

Code:
boolean found = false;
int p=0;
for (int k = 0; k < array1.length; k++){                    
  System.out.println("First For Loop");          
  String pID = array1[k].toStrin();                         
  System.out.println("pID>>>>>>>>" + pID);          
    for (p = 0; p < array2.length; p++){                
       System.out.println("Second For Loop");            
       String cID = array2[p].toStrin();                    
       System.out.println("cID>>>>>>>>" + cID);            
       if (pID.equals(cID)) {
         found=true;
         break;                
       }
    if (!found)
       delete array2[p];           
    }         
  }      
}

Cheers,
Dian
 
Sorry Guys..I posted this question wrong forum...
 
Thats OK, just repost it in forum269

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top