Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base: 312bd3770f
...
compare: eddbdc3155
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 43 additions and 0 deletions.
  1. +25 −0 chapter3/2.txt
  2. +18 −0 chapter3/4.txt
View
25 chapter3/2.txt
@@ -0,0 +1,25 @@
+//Making sure this works for both even and odd sized arrays
+
+scala> val arr = Array(11, 10, 1, 20, 2, 3, 5, 9)
+arr: Array[Int] = Array(11, 10, 1, 20, 2, 3, 5, 9)
+
+scala> arr.size
+res134: Int = 8
+
+scala> for(i<-0 to arr.size by 2 if i< arr.size-1) { val temp = arr(i); arr(i)=arr(i+1); arr(i+1)=temp }
+
+scala> arr
+res136: Array[Int] = Array(10, 11, 20, 1, 3, 2, 9, 5)
+
+scala> val arr = Array(11, 10, 1, 20, 2, 3, 5, 9,4)
+arr: Array[Int] = Array(11, 10, 1, 20, 2, 3, 5, 9, 4)
+
+scala> arr.size
+res137: Int = 9
+
+scala> for(i<-0 to arr.size by 2 if i< arr.size-1) { val temp = arr(i); arr(i)=arr(i+1); arr(i+1)=temp }
+
+scala> arr
+res139: Array[Int] = Array(10, 11, 20, 1, 3, 2, 9, 5, 4)
+
+scala>
View
18 chapter3/4.txt
@@ -0,0 +1,18 @@
+scala> val arr = Array(1,-2,3,-4,8,-7,6,10,-12,0,-1,-8)
+arr: Array[Int] = Array(1, -2, 3, -4, 8, -7, 6, 10, -12, 0, -1, -8)
+
+scala> val result = ArrayBuffer[Int]()
+result: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
+
+scala> arr.filter(_>0).foreach(result+=_)
+
+scala> result
+res151: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 3, 8, 6, 10)
+
+scala> arr.filter(_<=0).foreach(result+=_)
+
+scala> result
+res153: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 3, 8, 6, 10, -2, -4, -7, -12, 0, -1, -8)
+
+scala> result.toArray
+res154: Array[Int] = Array(1, 3, 8, 6, 10, -2, -4, -7, -12, 0, -1, -8)

No commit comments for this range

Something went wrong with that request. Please try again.