53

This is my code

    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?
    }

In java i would do something like this

someList.forEach(java.lang.System.out::print);

3
  • 12
    println(arr.contentToString()) Commented Feb 28, 2020 at 0:23
  • contentToString() is great Commented May 29, 2020 at 20:10
  • 1
    joinToString() is even better. Look at @delitescere's answer. Commented Oct 1, 2020 at 7:26

8 Answers 8

55

Idiomatically:

fun main(args: Array<String>) {
  val someList = arrayOf("United", "Chelsea", "Liverpool")
  println(someList.joinToString(" "))
}

This makes use of type inference, an immutable value, and well-defined methods for doing well-defined tasks.

The joinToString() method also allows prefix and suffix to be included, a limit, and truncation indicator.

Sign up to request clarification or add additional context in comments.

Comments

44

Array has a forEach method as well which can take a lambda:

var someList : Array<String> = arrayOf("United","Chelsea","Liverpool")
someList.forEach { System.out.print(it) }

or a method reference:

var someList : Array<String> = arrayOf("United","Chelsea","Liverpool")
someList.forEach(System.out::print)

2 Comments

arr.forEach(::println)
System.out.println( someList.joinToString() )
25

You can achieve this using "contentToString" method:

var someList : Array<String> = arrayOf("United","Chelsea","Liverpool")
  println(someList.contentToString())

O/p:
[United, Chelsea, Liverpool]e

Comments

11

I know three ways to do this:

(0 until someList.size).forEach { print(someList[it]) }
someList.forEach { print(it) }
someList.forEach(::print)

Hope you enjoyed it :)

Comments

4

You can do the same:

fun main(args : Array<String>){
    var someList : Array<String> = arrayOf("United","Chelsea","Liverpool")
    someList.forEach(System.out::print)
}

Comments

4

You could

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)
}

gives

United, Chelsea, Liverpool

alternatively you can use

print(element)

in the for loop, or even easier use:

var d = someList.joinToString()
println(d)

1 Comment

joinToString seems what I was looking for, the equivalent to Arrays.toString()
4

Simply do this, no need to use loop and iterate by yourself. Reference

println(someList.joinToString(","))

2 Comments

this should be the accepted answer.
@OkeUwechue println(someList.joinToString()) is even better because the default parameter for the delimiter is ", "
0

If it is solely for printing purpose then a good one liner is

 var someList : Array<String> = arrayOf("United","Chelsea","Liverpool")
 println(java.util.Arrays.toString(someList))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.