Linked Questions
222 questions linked to/from In detail, how does the 'for each' loop work in Java?
41
votes
2
answers
3k
views
What are for-each expressions in Java translated to? [duplicate]
for ( SomeListElement element : objectWithList.getList() ) { ... }
What is the above snippet translated to?
What I am mostly interested in is if the getList() method called once, or with each ...
19
votes
4
answers
27k
views
What is a difference between traditional loop and for-each loop? [duplicate]
I wonder if there is a difference between these:
ArrayList<Example> list = new ArrayList<Example>
1-)
for(int i = 0; i < list.size(); i++) {
list.get(i).doSomething();
}
2-)
for(...
14
votes
7
answers
98k
views
in a for-loop, what does the (int i : tall) do, where tall is an array of int [duplicate]
As the header says, I was tipped by some people that if I wanted to print the sum of everything in an array of numbers, I should use the above-mentioned parameter for a for-loop (code will follow if ...
15
votes
4
answers
89k
views
What does for(int i : x) do? [duplicate]
I am new to Java. I was reading someone's solution to a question and I encountered this:
int[] ps = new int[N];
for (int i = 0; i < N; i++)
ps[i] = input.nextInt();
...
8
votes
5
answers
3k
views
How does a for-each loop work? [duplicate]
How does a for-each loop works when it calls a method, either recursively or a different method?
Example:
for(String permutation : permute(remaining))
{
// Concatenate the first ...
3
votes
5
answers
2k
views
Why Looping through ArrayList<String> is same as a regular array? [duplicate]
Method 1
ArrayList<String> list = new ArrayList<String>();
for (String s : list) {
write.append(s);
write.append('\n');
}
How is looping through an ArrayList this way possible? ...
2
votes
6
answers
417
views
For loop in java not working [duplicate]
import java.util.Random;
public class RandomHomework
{
public static void main(String[] args)
{
int i;
Random generator = new Random();
double randomDecimals = ...
0
votes
7
answers
2k
views
For loop in Java [duplicate]
I am working in programming for last couples on years, but today I saw a new thing that I searched on net but can't find the perfect answer. There is a code which says
for(String string : mCha) // ...
7
votes
2
answers
2k
views
Is foreach loop literally rewritten to a for loop with iterator? [duplicate]
This post explains that foreach loop directly corresponds to using iterator. If I write a foreach loop is it literally going to be transformed into for with iterator? In particular, given loop:
for(...
1
vote
1
answer
4k
views
Is it a good practice to put String.toCharArray() into for loop [duplicate]
I came across code like below several times:
for (char c : s.toCharArray()) {
....
}
I'm wondering if s.toCharArray() only excuted once or excuted s.length times? And what about the case below?
...
-3
votes
7
answers
524
views
Array in method - JAVA [duplicate]
I saw this code in one of the tutorials.
There isn't any error with it, but I just don't understand the code.
Could someone guide me?
int trying[] = {3,4,5,6,7};
change (trying);
for(int y: trying) {...
-2
votes
3
answers
1k
views
Display the value of Varargs [duplicate]
In the Java Programming when we pass the all arguments from the varargs then how can i dispaly that argument value.. For example i saw a program something like this :
class var{
static void dis (...
0
votes
1
answer
1k
views
how does following for loop work ? for (String temp : uniqueSet) [duplicate]
Set<String> uniqueSet = new HashSet<String>(list);
for (String temp : uniqueSet) {
System.out.println(temp + ": " + Collections.frequency(list, temp));
}
Above is the java code that i ...
2
votes
1
answer
569
views
Implicit statement in a Java enhanced for loop [duplicate]
Since enhanced for loops are read only, it seems like each element is being copied into the new variable that you define when setting up the loop. Is there an implicit statement here? Maybe ...
-2
votes
1
answer
2k
views
Converting foreach loop to for loop [duplicate]
I'm really new to Java, and was wondering if someone could help me convert the following to foreach loops into for loops?
Assuming expression is a collection of items:
for(Type x: expression){
......