Linq style “For Each” [duplicate]

Using the ToList() extension method is your best option: someValues.ToList().ForEach(x => list.Add(x + 1)); There is no extension method in the BCL that implements ForEach directly. Although there’s no extension method in the BCL that does this, there is still an option in the System namespace… if you add Reactive Extensions to your project: using … Read more

How do I apply the for-each loop to every character in a String?

The easiest way to for-each every char in a String is to use toCharArray(): for (char ch: “xyz”.toCharArray()) { } This gives you the conciseness of for-each construct, but unfortunately String (which is immutable) must perform a defensive copy to generate the char[] (which is mutable), so there is some cost penalty. From the documentation: … Read more

Is there a difference between foreach and map?

Different. foreach iterates over a list and performs some operation with side effects to each list member (such as saving each one to the database for example) map iterates over a list, transforms each member of that list, and returns another list of the same size with the transformed members (such as converting a list … Read more

I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?

$begin = new DateTime(‘2010-05-01’); $end = new DateTime(‘2010-05-10’); $interval = DateInterval::createFromDateString(‘1 day’); $period = new DatePeriod($begin, $interval, $end); foreach ($period as $dt) { echo $dt->format(“l Y-m-d H:i:s\n”); } This will output all days in the defined period between $start and $end. If you want to include the 10th, set $end to 11th. You can adjust … Read more

Check if object value exists within a Javascript array of objects and if not add a new object to array

I’ve assumed that ids are meant to be unique here. some is a great function for checking the existence of things in arrays: const arr = [{ id: 1, username: ‘fred’ }, { id: 2, username: ‘bill’ }, { id: 3, username: ‘ted’ }]; function add(arr, name) { const { length } = arr; const … Read more

How to avoid java.util.ConcurrentModificationException when iterating through and removing elements from an ArrayList

Two options: Create a list of values you wish to remove, adding to that list within the loop, then call originalList.removeAll(valuesToRemove) at the end Use the remove() method on the iterator itself. Note that this means you can’t use the enhanced for loop. As an example of the second option, removing any strings with a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)