This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Mark Johnstone
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



/* 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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should be


and
 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for helping me correct the syntax. However, this does not resolve the problem I am facing. Please advise
 
Mark Johnstone
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not understood your statement, please could you explain in further detail
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got there in the end; this works. Please could you advise how I could improve the quality of the source?


 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic