scala实现相邻两个元素挑换位置的代码,哈哈

import scala.math._
import breeze.plot._
import breeze.linalg._

import scala.collection.mutable.ArrayBuffer
//https://stackoverflow.com/questions/36984780/spark-shell-how-to-use-breeze-viz
object breeze2 {
  def main(args: Array[String]): Unit = {
  }


  def pow(x: Double, n: Int): Double = {
    var pw = 1.0.toDouble
    var i = 1
    var m = n

    for (j <- 1 to (m / 2) if n % 2 == 0 && x > 0) {
      pw *= x
    }
    for (j <- 1 to m if n % 2 == 1 && x > 0) {
      pw *= x
    }
    for (j <- 1 to m if x < 0) {
      pw *= 1 / x
    }
    pw
  }

 // println(pow(3, 4))

  val b = ArrayBuffer(1, 7, 2, 9)
  val bSorted = b.sorted

  val a = ArrayBuffer(2,3,4,5,6,7)
    val i=0
for(i<-0 until  a.length)
  { for(j<-1 until a.length if (i==j-1) if(i%2==0))
    { val temp=a(i)
      a(i)=a(j)
      a(j)=temp
    }

  }


   for(t<- a)
     {
       println(a)
     }


  }

 

posted @ 2018-01-30 15:01  残阳飞雪  阅读(449)  评论(0编辑  收藏  举报