This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Nested for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Nested for loop " Watch "Nested for loop " New topic
Author

Nested for loop

shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76


Hi I am trying to check if the elements in a given array are Palindrome. The flow does not enter the second for loop( for(int j=b-1;j<=mid2;j--). Eclipse does not show any errors. Any ideas? Thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11250
    
  16

I would suggest the liberal application of "System.out.println()" statements. Print out what mid1 is just before you hit your outer loop, to see what it is actually looping over.

print what b-1 and mid2 are just before you enter your second loop to see what it is actually looping over.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

Hi shuba,

As per my point of view if possible avoid nested loop that will optimized performance of code

I written below code for palindrome in that I didn't used nested loop. You can refer below code.



Let see others members's opinion what they will suggest.


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7688
    
  19

shuba gopal wrote:Hi I am trying to check if the elements in a given array are Palindrome. The flow does not enter the second for loop( for(int j=b-1;j<=mid2;j--). Eclipse does not show any errors. Any ideas? Thanks

Yes. Look at that loop test again. What is your inner loop doing?

BTW, you don't need nested loops; it can all be done in one (and the length being odd or even is actually a red herring).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7688
    
  19

Ninad Kulkarni wrote:I written below code for palindrome in that I didn't used nested loop...

I'm relatively new here, so I'm not up on all the etiquette, but most of the forums that I belong to frown on full-code solutions. Far better to steer OP in the right direction and let the lightbulb come on by itself.

Winston
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11250
    
  16

Ninad Kulkarni wrote:
I written below code for palindrome in that I didn't used nested loop. You can refer below code.

Please don't provide full solutions like this. We want people to learn to code for themselves. Handing them a fully implemented answer doesn't really help them learn, just like handing someone a fish doesn't help them eat tomorrow.
shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76
Hi Fred,

I had used many System.out.println() statements. I had removed all of them just before posting here because they made the code look more complicated. That is how I found that the flow does not enter the second loop.

For the second loop, for(int j=b-1;j==mid2;j--) - b-1 = 5 and j=3. SOP just inside this loop is not printed. I just wanted to confirm if my usage is correct. Thanks Shuba
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11250
    
  16

Winston Gutkowski wrote:...most of the forums that I belong to frown on full-code solutions. Far better to steer OP in the right direction and let the lightbulb come on by itself.

Winston

as you can see, the moderators (especially me) agree with this 100%. In fact, it is even one of the points on our HowToAnswerQuestionsOnJavaRanch FAQ.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7688
    
  19

fred rosenberger wrote:...In fact, it is even one of the points on our HowToAnswerQuestionsOnJavaRanch FAQ.

So I notice.

BTW, I think the fact that those things are automatically linkable is great. Wish OTN did it.

Winston
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11250
    
  16

shuba gopal wrote:For the second loop, for(int j=b-1;j==mid2;j--) - b-1 = 5 and j=3. SOP just inside this loop is not printed. I just wanted to confirm if my usage is correct. Thanks Shuba

in the original code sample you posted, you have

for(int j=b-1;j<=mid2;j--)

but in your prior post, you say it is

for(int j=b-1;j==mid2;j--)

Which is correct?

Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

fred rosenberger wrote:Please don't provide full solutions like this. We want people to learn to code for themselves. Handing them a fully implemented answer doesn't really help them learn, just like handing someone a fish doesn't help them eat tomorrow.

Hi fred,
I agree with you and I will take care for the same in future.
Thanks for feedback
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7688
    
  19

shuba gopal wrote:For the second loop, for(int j=b-1;j==mid2;j--) - b-1 = 5 and j=3. SOP just inside this loop is not printed. I just wanted to confirm if my usage is correct. Thanks Shuba

Doesn't look like it to me. As I said before: What is you inner loop trying to do? If you think about that I suspect you'll get the answer.

Furthermore, I think you might do well to step back from your code and look at the original problem again (never a bad idea, even for us old hands).

What is a palindrome? What are its properties? If you start at the first character, what should it equal? And the 2nd? And the 3rd?.... Draw it out on a piece of paper and try it.
Do you see any pattern?

If so, write it down in English (or your native language) and then try to write a program based on it.

Winston

shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76
Fred, Ninad, Winston, thank you very much for responding to my question.

Winston, I finally figured out that the same logic applies when the number of elements are odd or even. I cant believe I missed this. Writing it out does clear things up. Many thanks.


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7688
    
  19

shuba gopal wrote:Winston, I finally figured out that the same logic applies when the number of elements are odd or even. I cant believe I missed this. Writing it out does clear things up. Many thanks.

And very well done to you. I bet you're pleased with yourself, and rightly so: a very nice, clear piece of code.

A little teaser for you: There are two things you could do to make it even tighter (and one of them will actually make it run faster too).
Can you spot what they are?

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
You should also reformat that code. { and } on lines by themselves. Single spaces before and after binary operators. It makes it much easier to read. Also use spaces, not tabs, for indenting.

Also try writing a recursive palindrome method
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3477
    
  13
Campbell Ritchie wrote:You should also reformat that code. { and } on lines by themselves.

Unless the style guide you are adhering to (correctly ) says that the { should be on the same line as the for, while, do, etc statement.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
I’m not letting you have the last word, Joanne Neal. Not even if I know you are right!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7688
    
  19

Campbell Ritchie wrote:I’m not letting you have the last word...

Actually, I'm with Joanne on that one. I generally stick me opening braces at the end of the line unless space dictates otherwise (and being an old COBOLer, I use column 72 as my limit ).

As for those guidelines, I don't agree with most of section 3 (particularly 3.1.2, which is plain wrong in my view), and also 2.3. I always make my constants all-caps - particularly if they're public - and the reasoning is bad too: when is a field called 'PI' likely to be refactored not to be a constant?.

However, all grist for the programming discussion mill .

Winston

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

I switched from 80 to 120 character maximums recently, because 80 made me jump through hoops at times to get a proper looking layout while still keeping under 80. 120 looked well enough on my screen. I agree on (most of) section 3 being rubbish.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
I did say Joanne was right. She and I have been winding each other up for ages.

We had a block of code unrelieved by spacing (except a blank line after one of the ifs) and it was simply difficult to read. Because you can’t see which } terminates the for, you cannot tell that the continue has no effect. I personally think the best convention about {} is what Cai Horstmann says, something like, “braces must match each other either vertically or horizontally.”

And I take no notice of bits in that style guide about capital letters for constants, not using do, not including i++ in expressions, etc. Agree about restricting column counts; we see too many bits of code here where you have to scroll right and left to read it. I usually add new lines to such code, but sometimes they use tabs for indenting, and that can make it look even worse after my editing
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
Section 3 appears to be what I have just said I take no notice of.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
shuba gopal wrote: . . . Winston, I finally figured out that the same logic applies when the number of elements are odd or even. . . .


You realise that you only need to traverse half an array to find out whether it is palindromic? If arr[0] == arr[arr.length - i], then arr[arr.length - i] == arr[0].
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7688
    
  19

Rob Spoor wrote:I switched from 80 to 120 character maximums recently, because 80 made me jump through hoops at times to get a proper looking layout while still keeping under 80. 120 looked well enough on my screen. I agree on (most of) section 3 being rubbish.

Ah, you're fond of reallyReallyDescriptiveAndDetailed() method/class/field names are you? Or do you like Builders?

Actually, me too (both); but I try to keep it to a dull roar .

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7688
    
  19

Campbell Ritchie wrote:I did say Joanne was right. She and I have been winding each other up for ages...

Yeah, I know. I was just joining her club.

BTW, the one major piece of advice that I think should be in every guideline on coding is conspicuously absent from that one:
Keep your methods short.

Other than Swing coding (which I loathe), I can't remember the last time I wrote a method that was more than about 60 lines long; and if I do, it's usually an indication that I haven't broken the problem down enough.

Winston
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3477
    
  13
Winston Gutkowski wrote:
Campbell Ritchie wrote:I’m not letting you have the last word...

Actually, I'm with Joanne on that one. I generally stick me opening braces at the end of the line unless space dictates otherwise

Well actually I disagree with that. Consistency is more important than where the { actually goes. If you need to split a line don't put just the { on a new line if you normally put it on the same line. Split the line somewhere else.

Thanks for posting that though. It means I (currently) have the last word on the subject
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7688
    
  19

Joanne Neal wrote:It means I (currently) have the last word on the subject

Not any more. I wrote a little ditty on this subject a while ago. Hope you don't mind if I share:

Newlines are for namby-pambies
Indentation too
Spaces for the faint of heart
So which of those are you?
Real coders know the score
that nothing could be finer
than to condense all their thought
into a one-liner.

Winston
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3477
    
  13
Don't give up the day job
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
Joanne Neal wrote:Don't give up the day job
I won’t.

But there is a school of thought, including many older C programmers (and FORTH programmers), who believe a program should occupy one line. If you can get the entire program into the () after for, so much the better.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Winston Gutkowski wrote:
Rob Spoor wrote:I switched from 80 to 120 character maximums recently, because 80 made me jump through hoops at times to get a proper looking layout while still keeping under 80. 120 looked well enough on my screen. I agree on (most of) section 3 being rubbish.

Ah, you're fond of reallyReallyDescriptiveAndDetailed() method/class/field names are you? Or do you like Builders?

Actually, me too (both); but I try to keep it to a dull roar .

Winston

The biggest problem is with long class names:
That's already 93 characters, and that's an easy example. Also, generics can have a big impact on line length. A method with generic type <T extends Comparable<? super T>> already required 33 characters for the type; if that method is public static that's already 51 characters including leading indentation of 4 spaces. That leaves very little space for the return type, method name and parameters.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
Winston Gutkowski wrote: . . .
BTW, the one major piece of advice that I think should be in every guideline . . .
Keep your methods short. . . .
More a design principle than style, but I agree. I read somewhere (forget where) that a method should be less than half a page, and a method which goes over a whole page really ought to be refactored.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Say, we're all going quite off-topic here. Can we get back to the palindrome issue, and have the code style discussion somewhere else?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38449
    
  23
Sorry. I do have a tendency to drift off topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested for loop