File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes if statement not correct? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "if statement not correct?" Watch "if statement not correct?" New topic
Author

if statement not correct?

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have following scenario:

I know, I manipulate the list "in" and "out" in the first loop. Is this the problem ? (Should I store the results of loop 1 in a temporary list?) (I know, I use a list instead of a set, maybe there are duplicates ???)

I guess, my statement in my second loop



is not correct.

But I cannot find the problem.


Any suggestions?
[ October 28, 2008: Message edited by: nimo frey ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I can't say I can really follow the logic here; I suspect there's a far simpler way to express what you're doing, if only I could understand what that is.

But in any case, that condition

if(!out.contains(s) && !in.contains(s))

will return false for any number that appears in either list (it's true if the number is not in either list). Even after your first manipulation loop, all the numbers 1 through 6 appear in at least one of "in" or "out"; so the condition should return false for any of these numbers, including the 3 and 6 that are in "notSelect."


[Jess in Action][AskingGoodQuestions]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

When you declare out, select, and notSelect you only are putting values into the 'in' list. The select and notSelect lists are completely empty. I think the code you wanted was:


Not positive if that fixes your problem, but it should be closer.


Steve
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

Yes, i too think the problem is with adding all the entries into list "in" while all the other list that are created are empty.


-kolli
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by Ravikanth kolli:
Yes, i too think the problem is with adding all the entries into list "in" while all the other list that are created are empty.


He will still always get false in the if statement because all the values in the notSelect list will also either be in the in or out list. So there is some logic error here. But at least he should be closer to solving the problem.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
okay, I will try to change the code and post it here

Maybe there will be better way to do this what I wanna do..
Don Solomon
Ranch Hand

Joined: Jul 20, 2008
Posts: 48
if you apply De Morgan's Law, for me at least, it is more understandable:

;therefore, expressing your condition in terms of negation, your results (empty set) is true.


Software development is an exercise in thinking not coding.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
okay thanks, the reason for my problem was, the order of my loops and the if-statement.

The following code works now (you can compile the method and see the result in the console)!

But I feel, there is some possibilities of optimizations, especially the many loops..can I express it with fewer Iterators?

Look at that:



Are there any tricks for saving few loops or making the code better
(Using Iterator, Comparator, removeAll or the kind...) ??

[ October 28, 2008: Message edited by: nimo frey ]
[ October 28, 2008: Message edited by: nimo frey ]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Yes, there probably is a better way to do what you want, but it is hard to see what you want to do. Please explain it in words.
Don Solomon
Ranch Hand

Joined: Jul 20, 2008
Posts: 48
i like the first program better, less code. i agree with steve, i would have to categorize the program requirements as: unknown.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

If you break into simpler parts, I think this is what you want the code to do:

1) If a number exists in the 'select' list
-- And if number does not exist in the 'in' list already
-- a) Add the number to the 'in' list
-- b) Remove the number from the 'out' list
2) If a number exists in the 'notSelect' list
-- And if the number does not exist in the 'out' list already
-- a) Add the number to the 'out' list
-- b) Remove the number from the 'in' list
3) The 'take' and 'notTake' lists appear only as intermediaries, they aren't what is wanted from the results.

If this is the case, I would take advantage of Collections math. Look into the addAll() and removeAll() methods to make a List that represents the 'difference' between 'select' and 'in'. Then add the 'difference' to 'in' and remove the 'difference' from 'out'. It would take about 4 lines of code, no loop.

Then duplicate substituting notSelect/out/in as needed. Finish with 8 lines of code (plus the lines for filling in the lists), and no loops.

Give it a try, see if you can get it.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
okay thanks I will try to do it with removeAll/addAll remove/add.

I will post the code, when I got it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
That sounds a good idea. Do you actually want to do it with Lists; that sort of operation is commonly regarded as a Set operation?
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
okay, I have done it well with Collection.Math! You can compile the method as it is and see the result.

The results of version 1 (with loops) and version 2 (without loops) are the same!!



I have a few questions:
1)
Should I prefer the collection.math instead of loops whenever possible? Why? Really faster? Lesser code, indeed!

2)
I do not use set (but all of my object in the list are unique). But in the case, if my list contains non unique values, are all dubs removed or only the first object which is find by the collection.math?

3)
Does the collection.math internally use loops to remove all elements?
If so, the version 1 should be better than version 2! Am I right?

4)
Can I express the version 2 with even fewer lines of code?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

1) Yes, don't re-invent the wheel. The code with Collections math is a lot cleaner and easier to read, and is less prone to common errors (logical problems with conditions, etc...)

2) Yes, as long as the collection being used follows the stringent contract of the Collections interface. Meaning at very least all the ones that are present in the standard Java API unless their documentation says otherwise. I believe some of the Queues in the java.util.concurrent package don't.

3) They may. But they would be well tested, and will generally be working on lower-level objects (for example arrays in an ArrayList). There would be next to zero impact on performance one way or another - these calls won't be a bottleneck and the clarity of the code would make it worthwhile even if there were some performance cost.

4) Yes, I think. Why do you do:

From the first few lines, take must be a sub-set of in. So when you do take.removeAll(in), take is emptied. Then you do out.removeAll(take) which does nothing (take is empty), and in.addAll(take) which does nothing (because take is still empty). The way I have this coded is:


This produces the same output in in and out as both of the versions you have, leaves select and notSelect unchanged, and substitutes difference for take or notTake (which I assumed weren't important since in both your versions they start and end empty).
[ October 29, 2008: Message edited by: Steve Luke ]
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
One short question before I provide a shorter version as version 2:

What is the difference between that:

1)


that:

2)


and that:

3)


Is 1) the same as 3 ? Call by value?

and 2) is not the same as 1 and 3? Call by reference?

Am I right?

So when I change the List difference of 2), then I change also the value of select?

When I change the List difference of 1) and 3), then I do NOT change the value of select? Am I right?
[ October 30, 2008: Message edited by: nimo frey ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Originally posted by nimo frey:
Is 1) the same as 3 ? Call by value?

More or less. Except for some possible optimizations (like 1 using a different initial capacity) they will result in the same: a copy of select. I always prefer 1 though, since it's shorter

and 2) is not the same as 1 and 3? Call by reference?

Am I right?

So when I change the List difference of 2), then I change also the value of select?

When I change the List difference of 1) and 3), then I do NOT change the value of select? Am I right?

With 2, you create another reference to the very same object. So all your statements I quoted are right.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Hmm, it seems, you are right!

Lets look at that:




I have:



I can change it something like:



or better:



or even better:




or at last:




So I can change this code of version 2:




or your code:




to this code (which produces the same result!?):



Am I right ?? I guess not..

[ October 30, 2008: Message edited by: nimo frey ]
[ October 30, 2008: Message edited by: nimo frey ]
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Hi Nimo

Lets look at that:


code:
--------------------------------------------------------------------------------

select.removeAll(in);//select = |select - in|take.addAll(select);// take = take+selectin.addAll(select);// in = in + selectout.removeAll(select);// out = |out-select|

--------------------------------------------------------------------------------




I have:


code:
--------------------------------------------------------------------------------

select = |select-in|take = take+selectin = in + selectout = |out-select|

--------------------------------------------------------------------------------



I can change it something like:


code:
--------------------------------------------------------------------------------

take = take + select-inin = in + select - inout = out-select + in


There is a problem with the above logic.
Lets say
in = {1,2,3}
select = {4,5,6}

Now
select.removeAll(in); //Result={4,5,6}
in.addAll(select); //Result={1,2,3,4,5,6}

So saying:

select.removeAll(in); => select = select - in IS RIGHT
in.addAll(select); => in = in + select IS RIGHT
But in = in + select => in + (select - in) => in = select IS WRONG
[ October 30, 2008: Message edited by: Satya Maheshwari ]

Thanks and Regards
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by nimo frey:
Hmm, it seems, you are right!

...

or your code:




to this code (which produces the same result!?):



Am I right ?? I guess not..



No, not the same. In my code:
difference = select - in
in = in + (select - in)
select = select

In your last bit of code:
select = select - in
take = take + (select - in)
in = select - in

in becomes just the part of select that wasn't already present in in.

I used this display code to double check values stored in the various lists just so I get to see what the output was:


If you try out your code, and display the results after each different implication you would see how the manipulation changes the output. Make sure you fiddle with the input values you put in the Lists to see what happens when you have duplicate values in a List, or when you have the same value in in/out or select/notSelect, etc... and make sure you get the output you expect in each list.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
That helped me really much! Thank you!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: if statement not correct?