How do you rotate (circular shift) of a Scala collection

后端 未结 11 1665
盖世英雄少女心
盖世英雄少女心 2020-12-19 03:40

I can do this quite easily, and cleanly, using a for loop. For instance, if I wanted to traverse a Seq from every element back to itself I would do the

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-19 04:16

    This is a simple piece of code

      object tesing_it extends App 
    {
    val one = ArrayBuffer(1,2,3,4,5,6)
    val  i = 2  //the number of index you want to move
    
    
    
     for(z<-0 to i){
       val y = 0
       var x =  one += one(y)
       x = x -= x(y)
       println("for seq after process " +z +" " + x)
      }
    
    
    println(one)
    
    }
    

    Result:

    for seq after process 0 ArrayBuffer(2, 3, 4, 5, 6, 1)

    for seq after process 1 ArrayBuffer(3, 4, 5, 6, 1, 2)

    for seq after process 2 ArrayBuffer(4, 5, 6, 1, 2, 3)

    ArrayBuffer(4, 5, 6, 1, 2, 3)

提交回复
热议问题