I have a Scala list of tuples, "params", which are of size 28. I want to loop through and print each element of the list, however, nothing is printed out. After finishing the for loop, I checked the size of the list, which now becomes 0.
I am new to scala and I could not figure this out after a long time googling.
val primes = List(11, 13, 17, 19, 2, 3, 5, 7)
val params = primes.combinations(2)
println(params.size)
for (param <- params) {
print(param(0), param(1))
}
println(params.size)
CodePudding user response:
combinations
methods in List create an Iterator
. Once the Iterator
is consumed using methods like size
, it will be empty.
From the docs
one should never use an iterator after calling a method on it.
If you comment out println(params.size)
, you can see that for loop is printing out the elements, but the last println(params.size)
will remain as 0.
CodePudding user response:
primes.combinations(2)
returns Iterator.
Iterators are data structures that allow to iterate over a sequence of elements. They have a hasNext method for checking if there is a next element available, and a next method which returns the next element and discards it from the iterator.
So, it is like pointer to Iterable collection. Once you have done iteration you no longer will be able to iterate again.
When println(params.size)
executed that time iteration completed while computing size
and now params is pointing to end. Because of this for (param <- params)
will be equivalent looping around empty collection.
There can 2 possible solution:
- Don't check the size before for loop.
- Convert iterator to Iterable e.g. list.
params = primes.combinations(2).toList
To learn more about Iterator and Iterable refer What is the relation between Iterable and Iterator?