Skip to content
Advertisement

Tag: for-loop

nested for loop for alphabet increment

I have searched everywhere and I cannot figure out how to create this output using a nested for loop in java: “a ab abc abcd” continued until z this is what I have tried String alphabet = “abcdefghijklmnopqrstuvwxyz”; Please help me! Answer Here is the answer: The outer loop switches from one row to another while the inner loop prints

Iterator vs for

I was asked in an interview what is the advantage of using iterator over for loop or what is the advantage of using for loop over iterator? Can any body please answer this? Answer First of all, there are 2 kinds of for loops, which behave very differently. One uses indices: This kind of loop isn’t always possible. For example,

Java 8 Iterable.forEach() vs foreach loop

Which of the following is better practice in Java 8? Java 8: Java 7: I have lots of for loops that could be “simplified” with lambdas, but is there really any advantage of using them? Would it improve their performance and readability? EDIT I’ll also extend this question to longer methods. I know that you can’t return or break the

for-loop very slow on Android device

I just ran into an issue while trying to write an bitmap-manipulating algo for an android device. I have a 1680×128 pixel Bitmap and need to apply a filter on it. But this very simple code-piece actually took almost 15-20 seconds to run on my Android device (xperia ray with a 1Ghz processor). So I tried to find the bottleneck

Java: for loop, incompatible types

I’m trying to run this for loop; However every time I try to compile I get an error stating ‘incompatible types – found int but expected boolean’ I can’t work out what I’m doing wrong! Answer the second statement: grid[0].length is an integer. The second statement in a for loop is a condition statement and needs to be a boolean.

A for-loop to iterate over an enum in Java

I have an enum in Java for the cardinal and intermediate directions: How can I write a for loop that iterates through each of these enum values? Answer .values() You can call the values() method on your enum. This values() method is implicitly declared by the compiler. So it is not listed on Enum doc.

Is there any way to do n-level nested loops in Java?

In other words, can I do something like Except N times? In other words, when the method creating the loops is called, it is given some parameter N, and the method would then create N of these loops nested one in another? Of course, the idea is that there should be an “easy” or “the usual” way of doing it.

Advertisement