这是我的代码
my_list = [1,2,2,3,4,0,3,4,4,1,3,1,4,4,1,0,0,0,0,0,4,2,3,2,2,1]
result = []
for i in range(len(my_list)):
if my_list[i] > 0:
for j in range(my_list[i]):
result.append(chr(97 + i))
else:
pass
print(result)
在java中我会做这样的事情
fun main(args : Array<String>){
var someList : Array<String> = arrayOf("United","Chelsea","Liverpool")
//How do i print the elements using the print method in a single line?
}
答案 0 :(得分:11)
var someList : Array<String> = arrayOf("United","Chelsea","Liverpool")
someList.forEach(System.out::print)
或方法参考:
if (name == a[i])
答案 1 :(得分:6)
您可以使用“ contentToString”方法实现这一点:
var someList : Array<String> = arrayOf("United","Chelsea","Liverpool")
println(someList.contentToString())
O/p:
[United, Chelsea, Liverpool]e
答案 2 :(得分:3)
我知道有三种方法可以做到这一点:
(0 until someList.size).forEach { print(someList[it]) }
someList.forEach { print(it) }
someList.forEach(::print)
希望你喜欢它:)
答案 3 :(得分:1)
您也可以这样做:
fun main(args : Array<String>){
var someList : Array<String> = arrayOf("United","Chelsea","Liverpool")
someList.forEach(System.out::print)
}
答案 4 :(得分:1)
你可以
fun main(args : Array<String>){
var someList : Array<String> = arrayOf("United","Chelsea","Liverpool")
val sb = StringBuilder()
for (element in someList) {
sb.append(element).append(", ")
}
val c = sb.toString().substring(0, sb.length-2)
println(c)
}
给出
United, Chelsea, Liverpool
或者你可以使用
print(element)
在for循环中,甚至更容易使用:
var d = someList.joinToString()
println(d)
答案 5 :(得分:1)
惯用地:
fun main(args: Array<String>) {
val someList = arrayOf("United", "Chelsea", "Liverpool")
println(someList.joinToString(" "))
}
这利用类型推断,不可变值和定义明确的方法来执行定义明确的任务。
jointoString()
方法还允许包含前缀和后缀,限制和截断指示符。