This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes comaparing two arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "comaparing two arrays" Watch "comaparing two arrays" New topic

comaparing two arrays

Anant Rao
Ranch Hand

Joined: Nov 12, 2004
Posts: 126
I have compare 2 arrays if they have same value then nothing has to done it array A contains and array B does not the have that value then write an insert statement and if array B contains and array A does not contain then write delete statement.

Can this be done in single loop or 2 loops. Can anybody help me to write this logic.

Any help would be apprecicated.

[ October 21, 2005: Message edited by: infyniti molugu ]
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
do you know about collection???
if not you might want to look at it, they got those kind of operation

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Will you process your items in sorted order? That becomes the classic master-update problem back from the days of punch cards when there was no random access and we had to process cards in sequence. It works on infinitely large datasets by reading from two sources (stacks of cards) without holding arrays of items in memory.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Anant Rao
Ranch Hand

Joined: Nov 12, 2004
Posts: 126
Thanks...but can u please have look what I did is correct...I am unable to use collection for second iteration ......

SelectedRoles is HashMap----that can be see in code
SelectedGroupRoles is String array

connection = getConnection();
//HashMap retrieveSelectedRoles = retrieveSelectedRoles(conn, wfdEntity.getWorkflowId());
HashMap selectedRoles = new HashMap();
selectedRoles.put("1", "Professor");
selectedRoles.put("2", "Guide");
Set keyItr = selectedRoles.keySet();
Iterator itr2 = keyItr.iterator();

for (int i = 0; i < SelectedGroupRoles.length; i++)
System.out.println("SelectedGroupRoles[i] :" + SelectedGroupRoles[i]);
if(! (selectedRoles.containsKey(SelectedGroupRoles[i]))){
System.out.println("Insert the key value" + SelectedGroupRoles[i]);

while (itr2.hasNext())
String strKey = (String);
for (int i = 0; i < SelectedGroupRoles.length; i++)
flag = false;
System.out.println("the value of gropuroles :" + SelectedGroupRoles[i]);
"strKey == SelectedGroupRoles[i] :"
+ strKey
+ " == "
+ SelectedGroupRoles[i]);

if (strKey.equals(SelectedGroupRoles[i]))
flag = true;
System.out.println("the vlaue of flag : " + flag);
if (!(flag))
System.out.println("Delete the key value " + strKey);

I agree. Here's the link:
subject: comaparing two arrays
It's not a secret anymore!