Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala阵列迭代非匹配输出_Scala - Fatal编程技术网

Scala阵列迭代非匹配输出

Scala阵列迭代非匹配输出,scala,Scala,这里使用迭代器迭代数组值,并逐个打印 或者[我的语言不好] 我想使用迭代逐个打印数组值 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){

这里使用迭代器迭代数组值,并逐个打印

或者[我的语言不好]

我想使用迭代逐个打印数组值

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

   }
}

在Scala中,避免使用索引处理集合更为惯用:

val myList = Array("a", "number", "of", "words") 
myList.foreach(s => println(s))
请注意,
Iterator(a)
创建了一个迭代器,其中包含一个元素
a

如果要使用元素的索引作为前缀:

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

// 0: a
// 1: number
// 2: of
// 3: words

在Scala中,避免使用索引处理集合更为惯用:

val myList = Array("a", "number", "of", "words") 
myList.foreach(s => println(s))
请注意,
Iterator(a)
创建了一个迭代器,其中包含一个元素
a

如果要使用元素的索引作为前缀:

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

// 0: a
// 1: number
// 2: of
// 3: words

myList-foreach-println
myList-foreach-println
thnx。。很多@Mila,顺便说一下,我在看你的另一个问题,你似乎在处理数组方面有困难。当您看到类似这样的内容时,
[Ljava.lang.String;@aad33f6
,这意味着您正在打印一个数组,而不是它的元素。数组索引为0,要访问数组的第一个元素
myList
,您可以执行
myList(0)
。要访问第i个元素,
myList(i)
.thnx..alot…@Mila,顺便说一句,我在看你的另一个问题时,你似乎在处理数组方面遇到了困难。当你看到这样的问题时
[Ljava.lang.String;@aad33f6
,表示您正在打印数组,而不是其元素。数组的索引位置为0,要访问数组
myList
的第一个元素,您可以执行
myList(0)
。要访问第i个元素,
myList(i)