Get the current index of a for each loop iterating an ArrayList
Just use a traditional for loop: for (int i = 0; i < yourArrayList.size(); i ++) { // i is the index // yourArrayList.get(i) is the element }
Just use a traditional for loop: for (int i = 0; i < yourArrayList.size(); i ++) { // i is the index // yourArrayList.get(i) is the element }
Explanation This error is often caused by passing a list to for_each, but for_each only works with unordered data-types, i.e. with sets and maps. Solution The resolution depends on the situation. List of strings If the list is just a list of strings, the easiest fix is to add a toset()-call to transform the list … Read more
Assuming K is your key type and V is your value type: for (Map.Entry<K,V> entry : map.entrySet()) { K key = entry.getKey(); V value = entry.getValue(); // do stuff }
Use this (it is loop of reading each line from file file) cat file | while read -r a; do echo $a; done where the echo $a is whatever you want to do with current line. UPDATE: from commentators (thanks!) If you have no file with multiple lines, but have a variable with multiple lines, … Read more
Yes, it’s perfectly safe. From [class.temporary]/4-5: There are two contexts in which temporaries are destroyed at a different point than the end of the fullexpression. The first context is when a default constructor is called […] The second context is when a reference is bound to a temporary. The temporary to which the reference is … Read more
The indexer will return a copy of the value. Making a change to that copy won’t do anything to the value within the dictionary… the compiler is stopping you from writing buggy code. If you want to do modify the value in the dictionary, you’ll need to use something like: // Note: copying the contents … Read more
A slightly more efficient way to do this: Map<MyClass.Key, String> data = (HashMap<MyClass.Key, String>) getData(); StringBuffer sb = new StringBuffer(); for (Map.Entry<MyClass.Key,String> entry : data.entrySet()) { sb.append(entry.getKey()); sb.append(“: “); sb.append(entry.getValue()); } return sb.toString(); If at all possible, define “getData” so you don’t need the cast.
It’s not possible to loop backwards using the for each loop syntax. As an alternative you can use a For i = a To 1 Step -1 loop: Sub reverseForEach() Dim i As Long, rng As Range Set rng = ActiveSheet.Range(“A1:B2”) For i = rng.Cells.Count To 1 Step -1 Debug.Print rng.item(i).Address ‘ Or shorthand rng(i) … Read more
The map function returns an array of items and forEach just loop over them. To make this code work use : render() { const items = []; this.props.items .forEach(item => items.push( <li> <TodoItem id={item.id} key={item.id} text={item.text} /> </li> )) return( <ul>{items}</ul> ); }