0

Here array values are iterate using Iterator and print one by one.

or [My language is not good]

i want to print array values one by one using iteration

package com.aitrich.collection

object IteratorDemo {
def main(args: Array[String]) {

      var myList = Array("a", "number", "of", "words")  

      var l=myList.length

      for( i <- 0 to l){
      var Lst:String=myList.toString()
      val it = Iterator(l.formatted(Lst))
      while (it.hasNext){
         println(it.next())
      }
      }

   }
}

but the output shows

[Ljava.lang.String;@aad33f6
[Ljava.lang.String;@aad33f6
[Ljava.lang.String;@aad33f6
[Ljava.lang.String;@aad33f6
[Ljava.lang.String;@aad33f6
1
  • 2
    myList foreach println Commented Aug 17, 2013 at 5:23

1 Answer 1

3

In Scala it is more idiomatic to avoid using index to work on collections:

val myList = Array("a", "number", "of", "words") 
myList.foreach(s => println(s))

Note that Iterator(a) creates an iterator with a single element a in it.

If you want to prefix with the index of the element:

myList.zipWithIndex.foreach{ case (s, i) => println(s"$i: $s")}

// 0: a
// 1: number
// 2: of
// 3: words
Sign up to request clarification or add additional context in comments.

2 Comments

thnx.. alot.. .
@Mila, By the way I was looking at your other question and you seem to be having difficulties with dealing with arrays. When you see something like this [Ljava.lang.String;@aad33f6, it means you are printing an array, not its element. Arrays are indexed at 0, and to access the first element of array myList you can do myList(0). To access the ith element, myList(i).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.