my dog learned polymorphism*
The moose likes Beginning Java and the fly likes simple program - unable to remove ALL duplicates when comparing 2 different arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simple program - unable to remove ALL duplicates when comparing 2 different arrays" Watch "simple program - unable to remove ALL duplicates when comparing 2 different arrays" New topic
Author

simple program - unable to remove ALL duplicates when comparing 2 different arrays

Mark Johnstone
Greenhorn

Joined: Sep 25, 2010
Posts: 17



/* Output displays
L1 4
L1 5
L1 7
L1 9
L1 10
L2 1
L2 7
L2 15
L2 552
L2 91
*/

Unable to figure out why seven is not being compared
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Hi Mark and welcome to the JavaRanch.

Please UseCodeTags when posting code. It makes it much easier to read and it will increase the number of people helping you.
I would like to give you to 2 hints:

Use a nested for loop:

And look at ArrayList. It contains a very useful method.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38077
    
  22
Wouter Oet wrote:Hi Mark and welcome to the JavaRanch.

Please UseCodeTags when posting code.
Agree. Since you are new I shall edit your post and you can see how much better it looks.

Don't use the == operator, nor !=, to compare reference types for equality.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

This should be


and


SCJP6.0,My blog Ranchers from Delhi
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38077
    
  22
Shanky Sohar wrote:This should be
It would be better asbecause there is no risk of the value left of the dot beingnull. I think one pair of () in that line of code are redundant.
Mark Johnstone
Greenhorn

Joined: Sep 25, 2010
Posts: 17
Thank you for helping me correct the syntax. However, this does not resolve the problem I am facing. Please advise
Mark Johnstone
Greenhorn

Joined: Sep 25, 2010
Posts: 17
I have amended the syntax as per say but how can I increment the index for an array and delete array elements please see below:
Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49
Hi,

This can be done with two for loops as told by 'Wouter' . Try to implement the steps below. It will work for you.

1. For loop for 1st string array.
2. Inside the for loop add one for loop for the the 2nd string array.
3. inside that compare the element of 1st and 2nd array.
If true----> make both ""
if false----> do nothing
4. After that print the array as you are doing in your code.

Regards,
Sudipta Kumar Laha
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Campbell Ritchie wrote:
Shanky Sohar wrote:This should be
It would be better asbecause there is no risk of the value left of the dot beingnull. I think one pair of () in that line of code are redundant.



Agreed both of them work and it is better we use the one told by you..infact we always use what you told

Mark Johnstone
Greenhorn

Joined: Sep 25, 2010
Posts: 17
Despite making the changes suggested I still get the wrong output even thought the program appears logical




Output:

L1 3
L1 4
L1 5
L1 6
L1 7
L1 8
L1 9
L1 10
L2 1
L2 3
L2 6
L2 7
L2 8
L2 15
L2 552
L2 91

Please advise
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

That's a tricky one. The for-each loop is syntactic sugar. It becomes something like:

So when you assign a new value to elementFromList1 you're not changing anything.
A solution would be removing/replacing the item from the list or to check out the other hint I posted.
Mark Johnstone
Greenhorn

Joined: Sep 25, 2010
Posts: 17
I have not understood your statement, please could you explain in further detail
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

The for each loop isn't a special construct. It's syntactic sugar. It gets compiled to something like the code I posted (I don't know the precise technical construct). And because of that the variable provided by the for each loop is just another reference to the element in the collection. And you can change that reference, like you're doing, but it won't change anything on the element in the collection.
Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49
Hi..




use normal for loop instead for enhanced for loop in your last code. It will work.

In enhanched for loop the assignment will not work as you did.

Mark Johnstone
Greenhorn

Joined: Sep 25, 2010
Posts: 17
Got there in the end; this works. Please could you advise how I could improve the quality of the source?


Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Your array is called list: that causes confusion.
It only contains Integers yet the type is String.

You're setting the value to an empty String and later you're output any value but the empty String.
Why not use an List and remove the duplicates?

Break-up the method into smaller methods.
Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49
hi,

yup i agree with 'Wouter'.
You can also see that when you remove data(by mentioning "") then there is wastage of memory. So if you use list the issue will be resolved.
 
Don't get me started about those stupid light bulbs.
 
subject: simple program - unable to remove ALL duplicates when comparing 2 different arrays
 
Similar Threads
struct in java
Comparing Array Elements
Khalid's Mock Test.
91%
sorting arraylist of arraylist