frank
04/28/2024, 3:35 AMval numbers = arrayOf("1", "2")
Stream.of(numbers).forEach { println(it) } // Prints all Array
// Prints out the full array:
// [Ljava.lang.String;@2121f1bb
Stream.of("1", "2").forEach { println(it) } // Prints each item
// Prints out every item:
// 1
// 2
Ruckus
04/28/2024, 3:51 AMStream.of(*numbers)
to get the save behavior as Java. You can read about it in the docs (look for where it talks about the spread operator).Ruckus
04/28/2024, 5:37 AMRuckus
04/28/2024, 5:40 AMval numbers = arrayOf("1", "2")
Stream.of("1", "2") // results in Stream<String> with 2 elements
Stream.of(numbers) // results in Stream<Array<String>> with 1 element
Stream.of(*numbers) // results in Stream<String> with 2 elements by "spreading" numbers to the vararg
Ruckus
04/28/2024, 5:48 AMString[] numbers1 = {"1", "2"};
String[] numbers2 = {"3", "4"};
Stream.of(numbers1, numbers2); // results in Stream<String[]> with 2 elements
Stream.of(numbers1); // results in Stream<String> with 2 elements due to implicit spreading
And:
void count(Object... objs) {
System.out.println(objs.length);
}
count(null, null, null); // prints 3
count(null, null); // prints 2
count(null); // throws NullPointerException
count(); // prints 0
Not to mention issues with primitive type arrays. You can read more in this SO answer.