gpt4 book ai didi

scala - 如何在 Scala 中打印任何内容的列表?

转载 作者:行者123 更新时间:2023-12-03 11:03:44 24 4
gpt4 key购买 nike

目前我有一个打印 Ints 的方法

def printList(args: List[Int]): Unit = {
args.foreach(println)
}

如何修改它以便它足够灵活以打印任何内容的列表?

最佳答案

println适用于任何事情:

def printList(args: List[_]): Unit = {
args.foreach(println)
}

甚至更好,因此您不仅限于 List s:
def printList(args: TraversableOnce[_]): Unit = {
args.foreach(println)
}

关于scala - 如何在 Scala 中打印任何内容的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6639377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com