publicclass T6238612 { publicstaticvoid main(String... args) { new T6238612().test();
}
boolean error = false;
// exercise the List.toArray method for a variety of lists void test() {
test(List.<String>nil());
test(List.of("a"));
test(List.of("a", "b", "c"));
test(List.of("a", "b", "c", "d", "e", "f")); if (error) thrownew Error("test failed");
}
// given a list, exercise the List.toArray method for a variety of arrays void test(List<String> list) { int n = list.size(); if (n > 0)
test(list, new String[0]);
if (n > 1)
test(list, new String[n - 1]);
test(list, new String[n]);
test(list, new String[n + 1]);
test(list, new String[n + 5]);
}
// test the List.toArray method for a particular list and array void test(List<String> list, String[] array) {
String[] result = list.toArray(array);
if (result.length < list.size()) {
error("returned array is too small; expected: " + list.size() + ", found: " + result.length); return;
}
if (list.size() <= array.length && result != array)
error("new array wrongly created");
int i = 0; for (String s: list)
check(result, i++, s);
if (i < result.length)
check(result, i, null);
}
// check a specific element of an array void check(String[] array, int i, String expected) { if (!equal(array[i], expected))
error("element " + i + " incorrect; expected: " + str(expected) + ", found: " + str(array[i]));
}
// check if two strings are both null or are equal boolean equal(String s1, String s2) { return (s1 == null ? s2 == null : s1.equals(s2));
}
String str(String s) { return (s == null ? "null" : '"' + s + '"');
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.