Loop over multiple arrays simultaneously: Difference between revisions

Content added Content deleted
Line 1,593: Line 1,593:
=={{header|Ecstasy}}==
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
module LoopOverMultipleArrays
module LoopOverMultipleArrays {
{
void run() {
@Inject Console console;

void run()
{
Char[] chars = ['a', 'b', 'c'];
Char[] chars = ['a', 'b', 'c'];
String[] strings = ["A", "B", "C"];
String[] strings = ["A", "B", "C"];
Int[] ints = [ 1, 2, 3 ];
Int[] ints = [ 1, 2, 3 ];


@Inject Console console;
console.print("Using array indexing:");
console.print("Using array indexing:");
for (Int i = 0, Int longest = chars.size.maxOf(strings.size.maxOf(ints.size)); i < longest; ++i)
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() : ""}\
console.print($|{i < chars.size ? chars[i].toString() : ""}\
|{i < strings.size ? strings[i] : ""}\
|{i < strings.size ? strings[i] : ""}\
|{i < ints.size ? ints[i].toString() : ""}
|{i < ints.size ? ints[i].toString() : ""}
);
);
}
}


console.print("\nUsing array iterators:");
console.print("\nUsing array iterators:");
Line 1,616: Line 1,613:
val stringIter = strings.iterator();
val stringIter = strings.iterator();
val intIter = ints.iterator();
val intIter = ints.iterator();
while (True)
while (True) {
{
StringBuffer buf = new StringBuffer();
StringBuffer buf = new StringBuffer();
if (Char ch := charIter.next())
if (Char ch := charIter.next()) {
{
buf.add(ch);
buf.add(ch);
}
}
if (String s := stringIter.next())
if (String s := stringIter.next()) {
{
s.appendTo(buf);
s.appendTo(buf);
}
}
if (Int n := intIter.next())
if (Int n := intIter.next()) {
{
n.appendTo(buf);
n.appendTo(buf);
}
}
if (buf.size == 0)
if (buf.size == 0) {
{
break;
break;
}
console.print(buf);
}
}
console.print(buf);
}
}
}
}
}
</syntaxhighlight>
</syntaxhighlight>