39 lines
1.3 KiB
Plaintext
39 lines
1.3 KiB
Plaintext
module LoopOverMultipleArrays {
|
|
void run() {
|
|
Char[] chars = ['a', 'b', 'c'];
|
|
String[] strings = ["A", "B", "C"];
|
|
Int[] ints = [ 1, 2, 3 ];
|
|
|
|
@Inject Console console;
|
|
console.print("Using array indexing:");
|
|
for (Int i = 0, Int longest = chars.size.maxOf(strings.size.maxOf(ints.size));
|
|
i < longest; ++i) {
|
|
console.print($|{i < chars.size ? chars[i].toString() : ""}\
|
|
|{i < strings.size ? strings[i] : ""}\
|
|
|{i < ints.size ? ints[i].toString() : ""}
|
|
);
|
|
}
|
|
|
|
console.print("\nUsing array iterators:");
|
|
val charIter = chars.iterator();
|
|
val stringIter = strings.iterator();
|
|
val intIter = ints.iterator();
|
|
while (True) {
|
|
StringBuffer buf = new StringBuffer();
|
|
if (Char ch := charIter.next()) {
|
|
buf.add(ch);
|
|
}
|
|
if (String s := stringIter.next()) {
|
|
s.appendTo(buf);
|
|
}
|
|
if (Int n := intIter.next()) {
|
|
n.appendTo(buf);
|
|
}
|
|
if (buf.size == 0) {
|
|
break;
|
|
}
|
|
console.print(buf);
|
|
}
|
|
}
|
|
}
|