Java8 For Each Loop Skip first Iteration - list

How can I write a code JAVA8 For each loop to skip first iteration. I have gone through previous answers in Stack Overflow. But all were Java <8 versions.
cars.forEach(car->{
//Skip first then do some thing
})

cars.stream()
.skip(1)
.forEachOrdered...
Or:
Spliterator<Car> sp = cars.stream().spliterator();
sp.tryAdvance(x -> {
// do nothing
});
sp.forEachRemaining(System.out::println);

Other way is use subList method
cars.subList(1,cars.size()).forEach() ...

Related

Does the number of iterations of the 'for' loop change if the second argument changes in one of the iterations?

i've this code here:
for(i = openGates[0]; i < closeGates[0]; i++) {
if(str[i] == '(') {
closeGates.removeFirst();
openGates.removeAt(1);
}
}
If brace found, closeGates[0]'s value will change. Will it change the number of iterations?
Yes, the iteration-expression is executed after every iteration of the loop.
Reference for this answer :
https://en.cppreference.com/w/cpp/language/for
Apparently, no one decided to answer and I want to close this question so: yes, in C++ 'for' loop evaluates its conditions before every iteration thus the number of the iterations might change.

range function in sml with a step parameter

I am very new to SML and functional programming.
I have searched the site but was unable to find an answer to my question.
I am trying to write a basic range function with a start, stop, and step parameter.
For example, range(2, 12, 3) should return the list [2,5,8,11].
I don't get any errors, but when I try running range(2,12,3); the cursor advances to the next line and nothing happens, I can't even type anything into the smlnj app.
Here is my code:
fun range(start, stop, step) =
if start = stop then nil
else start::range(start+step, stop, step);
Which outputs this:
val range = fn : int * int * int -> int list
What changes do I need to make to my code so that when I run range(2,12,3) I get [2,5,8,11] ?
Thank you
Your condition (start = stop) to break the recursion is wrong. In your example you'll perform recursive calls with start=2, start=5, start=8, start=11, start=14, ... leading to an infinite loop. It's not that nothing happens, it's just that smnlj keeps computing...
You have an infinite recursion whenever stop - start is not a multiple of the step size (or zero).
Look at range(11,12,3), which should generate your last number:
if 11 = 12 then nil
else 11::range(11+3, 12, 3)
This will calculate range(14,12,3):
if 14 = 12 then nil
else 14::range(14+3, 12, 3)
and then range(17,12,3), and so on, ad infinitum.
You need to replace = with either > or >=, depending on whether stop should be included or not.

Reverse For loop skips over completely if there is only one entry?

I have an array I am reverse looping through. However, it seems if there is only one entry in my array, it doesn't loop at all.
for (uint8 i = fileNames.Num() - 1; i --> 0;)
{
//Do stuff
}
Can anyone tell me why that is? Or what I can do to fix the loop conditions?
The code you posted means the same thing as the following:
uint8 i = filenames.Num() - 1
while ( (i--) > 0) {
//do stuff
}
If filename.Num() is 1 then the loop body does not execute because 1 - 1 is not greater than zero.
This expression i --> 0 is not idiomatic C++ and has no place in real code. It is a programming language pun, and to some extent a joke someone posted on Usenet at the expense of newbies akin to sky hooks or snipe hunts in other social domains e.g. "Hey did you know about the secret operator in C++???"

Why is the first digit of a string still read and output in a while loop where the condition !isdigit is false?

Note: I know the solution that would solve the issue, but I don't understand the "computer logic," or what is going on behind the compiler.
In my C++ textbook, there is a blurb of example code that reads:
cout << "Enter a line of input:\n";
char next;
while ((!isdigit (next)) && (next != '\n'))
{
cin.get (next);
cout << next;
}
cout << "<END OF OUTPUT>";
... Paired with the example input: I'll see you at 10:30 AM.
I would expect, after typing in the input, for the output to be I'll see you at <END OF OUTPUT>. Instead, the first digit, "1," is also output, such that it becomes I'll see you at 1<END OF OUTPUT>. Why is this? Is this because the statement cin.get (next); is not declared/initialized outside of the while loop, and thus the first next value tested in the while loop's conditional parameters is not actually the first character of the keyboard input? Or does the while loop run an extra iteration when the next condition is not satisfied?
If it's the former, what does the computer test if next is not set to a value? And why does the first digit ("1") read still meet the condition to run the loop again one more time before terminating?
As mentioned in the comments, you have to initialize next before using it, otherwise it is undefined behavior.
Now lets assume, next is properly initialized to a non-digit character. The condition (!isdigit (next)) && (next != '\n') is checked once when you enter the while loop and every time when you reach the end of the statement in curly braces. In your first version, you get a new char and immediately stream it to cout. The check is done afterwards and the loop terminates as expected.

problem in using list in game maker studio

I am new to the game maker. I created a list and I want to compare all the data in the list with a specific value. I used the following code:
for(var i=0;i<ds_list_size(lst);i++;)
{
if ds_list_find_value(lst,i)>tmp
ds_list_replace(lst,i,ds_list_find_value(lst,i)-1);
}
and I face the following error:
Push :: Execution Error - Variable Get -1.lst(100001, -1)
at
gml_Object_object0_RightButtonPressed_1 (line 21) - for(var i=0;i
where is my problem?
Thanks all.
if your first for loop i = 0; and when the first entry in the list is smaller than tmp it tries to replace the first place in the list with a not existing one. so you could either check if its the first entry of the list with
if ( i == 0 ) { }
or your could start the for loop from the second entry with
for(var i=1;i<ds_list_size(lst);i++;)
I think the ; at the end of i++; is unnecessary, you only need to use ; in a for-loop as seperator.
GML gives more freedom to common C# rules though (like how there are no brackets needed around an if-condition), so perhaps that's allowed.
Another possibility might be that the index is out of range at ds_list_replace()