Operation of a packet array scala

Usually we have some of the demand for an array of pairwise flip, usually involve odd and even, otherwise there will be an array index abnormal, so how can we do it? Although it is an entry-level problem, but I still think this is a very interesting topic, so he wrote a generic count operation for the array, do question the future can copy and paste directly.

 

the Array of arr1 = Val (. 1, 2,. 3,. 4,. 5 ) 
  Val arr2 is = an ArrayBuffer [Int] () 
  Val NUM =. 3
   for (I <- an until 0 (arr1.length / + NUM. 1); NUM J = I * ; K <- (J an until (J + . NUM)) Reverse) {// use num + 1 is calculated in the end the number of groups
     the try { 
      arr2 is + = of arr1 (K) 
    } 
    the catch {
       Case EX: an ArrayIndexOutOfBoundsException => { 
      } 
    } 
  } 
  Print (arr2 is) 

output: [3,2,1,5,4]

 

Guess you like

Origin www.cnblogs.com/tjpeng/p/11613130.html