This question already has answers here:
Java, How do I get current index/key in "for each" loop [duplicate]
(6 answers)
Closed 7 years ago.
I've been wondering if it is somehow possible to get the current index of a for each loop in Java without a seperate count variable.
In debug mode I can see the loop in the following structure:
i$ = {ArrayList$Itr#17838}
cursor = 1
lastRet = 0
expectedModCount = 5
this$0 = {ArrayList#17836}
So I guess cursor is the index. Is there a way to access the cursor?
Thanks for your help
So far
Not to my knowledge. But even if there were a way, it would be counterproductive to access. The reason why one would want a for:each loop is because it is quick, and cycles through a whole array in a simple manor. If you want to have a cursor index to access, use a for loop and just access the incrementing variable.
Related
This question already has answers here:
How to use subList()
(6 answers)
Closed 3 years ago.
I want to pass an array as argument to a function without first element.
I came up with this solution, but I'm wondering if there are better ways to it.
List<Integer> numbers = new ArrayList<>();
//... in the meantime numbers is an array that contain 1000 elements;
numbers.remove(0);
myFunction(numbers)
You can use subList(firstElement, lastElement); method.
Here please check javadoc.
This question already has answers here:
Difference between moving an Iterator forward with a for statement and a while statement
(5 answers)
Closed 5 years ago.
hello i'm trying ton understand a programm and a line giving me some trouble. Can you explain me the Construction of this line because i have never seen a "For" like that
for (Iterator<IBaseVO> itMachine = machinesSelected.iterator(); itMachine.hasNext();) {
MachineVO mach = (MachineVO) itMachine.next();
idsMachines.add(mach.getMchId());
}
thanks you
A for loop can have 3 arguments, but it's not required.
Usually goes like this
instruction
boolean
instruction
the first and the third one can be omitted safely.
So here is the first instruction slot used to initialize the iterator object.
The second instruction slot is used to see if there are new items in the iterator.
the third slot is not needed and thusly omitted.
This does the same as
Iterator<IBaseVO> itMachine = machinesSelected.iterator();
while (itMachine.hasNext()) {
MachineVO mach = (MachineVO) itMachine.next();
idsMachines.add(mach.getMchId());
}
This question already has answers here:
How does a for loop work, specifically for(;;)?
(6 answers)
Closed 7 years ago.
There are a lots of option available for infinite loop but this are mostly use while(true) or for(;;)
I know while(true) is the best option, since it is easier to understand.
But I want to use for(;;).
I want to know what is going on inside of for loop when I we used two ; within for loop.
for(;;)
Semicolon means its an empty statement. But how its works when we use inside of for loop for infinite execution?
This is what happens:
for (initialization statement; condition check; increment/decrement)
// loop body;
With for(;;):
There is no initialization.
There is no condition check.
There is no increment/decrement.
Therefore it will run forever, exactly like while(true).
This question already has answers here:
Remove multiple elements from ArrayList
(16 answers)
Closed 7 years ago.
I have an array of indices at which elements must be removed from a List, e.g.: (4), (7), (8).
The problem is:
1) Can't use a For-loop, the size changes after each delete (ArrayList.remove(i))
2) Can't use an Iterator with an updated counter, the counter will also not work anymore (Iterator.remove()).
Found a nice solution here: Remove multiple elements from ArrayList
Collections.sort(indices, Collections.reverseOrder());
for (int i : indices)
strs.remove(i);
That seems to be the only fast and simple solution. Reverse-sort first, then go through the indices and remove.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have a program that I am writing and I have it completed but I keep receiving an error in a counter that I am using. The purpose of the program is to build a plate stacker and I have finished that but need to write a counter based on the particular colors used (how many green plates...etc.). As I know several classmates use this site to cheat, I will not post the code in whole but only the fragments I feel are necessary. Below is an example of a statement that I am using to attempt to get a count of the beige plates in the text file. The statement is in a while loop with the other colors having the same coding. I also have initialized the counter (int beige = 0;) prior to this and outside of the while loop:
if(line == "beige")
{
beige++;
}
I have a System.out following these statements to display the count but it always comes up as 0 so I believe the problem is in this particular fragment. If I need to send additional coding I will but I believe it is something to do with this or the placement of it in the while loop.
First, you are comparing the string in a wrong way. Use .equals method.
if (line.equals("beige"))
beige++;
Second, you can use HashMap for this kind of problem where you keep updating the value(count) for key(color) and it will save you a lot of if conditions if say there are a hundred different colors!