I'm trying to translate the code from Java to Scala:
class Item {
public long lng;
public String str;
Item(long lng, String str) {
this.lng = lng;
this.str = str;
}
}
Item[] items = getItems(...) //doesn't really matter
int maxCount = getMaxCount()
for(int i = 0; i < maxCount && i < items.length; ++i) {
System.out.println(String.format("%d %s",items[i].lng, items[i].str));
}
Here is my attempt:
class Item (val lng: Long, val str: String)
val items = Array(new Item(1, "11"), new Item(2, "22"))
val maxCount = getMaxCount
for {
x <- items
i <- 0 to maxCount
if i < maxCount
} println(x.lng.toString + " " + x.str)
Also, how can do that without for {...}
using filterWith
and foreach
but without var
?
It prints the string maxCount*2
times.