Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scala 语言翻译 #1488

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions codes/scala/chapter_array_and_linkedlist/array.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@

/**
* File: Array.scala
* Author: WangBlue (wangjiping596@gmail.com)
* Create: 2024-08-10 11:09
*/
class array {
/* 随机访问元素 */
def randomAccess(nums: Array[Int]): Int = {
// 在区间 [0, nums.length) 中随机抽取一个数字
val index: Int = scala.util.Random.nextInt(nums.length)
// 获取并返回随机元素
nums(index)
}

/* 扩展数组长度 */
def extend(nums: Array[Int], enlarge: Int): Array[Int] = {
// 初始化一个扩展长度后的数组
val res: Array[Int] = new Array[Int](nums.length + enlarge)
// 将原数组中的所有元素复制到新数组
for (i <- nums.indices) {
res(i) = nums(i)
}
// 返回扩展后的新数组
res
}

/* 在数组的索引 index 处插入元素 num */
def insert(nums: Array[Int], num: Int, index: Int): Unit = {
// 把索引 index 以及之后的所有元素向后移动一位
for (i <- nums.length - 1 to index by -1) {
nums(i) = nums(i - 1)
}
// 将 num 赋给 index 处的元素
nums(index) = num
}

/* 删除索引 index 处的元素 */
def remove(nums: Array[Int], index: Int): Unit = {
// 把索引 index 之后的所有元素向前移动一位
for (i <- index until nums.length - 1) {
nums(i) = nums(i + 1)
}
}

/* 遍历数组 */
def traverse(nums: Array[Int]): Unit = {
var count: Int = 0
// 通过索引遍历数组
for (i <- nums.indices) {
count += nums(i)
}
// 直接遍历数组元素
for (num <- nums) {
count += num
}
}

/* 在数组中查找指定元素 */
def find(nums: Array[Int], target: Int): Int = {
for(i <- nums.indices) {
if(nums(i) == target) {
return i
}
}
-1
}
}


object array {
def main(args: Array[String]): Unit = {

// // 初始化数组
// var arr: Array[Int] = new Array[Int](5)
// var nums: Array[Int] = Array(1, 3, 2, 5, 4)


val myArray = new array()
/* 初始化数组 */
val arr: Array[Int] = new Array[Int](5)

println("数组 arr = " + arr.mkString(" "))

var nums: Array[Int] = Array(1, 3, 2, 5, 4)
println("数组 nums = " + nums.mkString(" "))

/* 随机访问 */
val randomNum = myArray.randomAccess(nums)
println("在 nums 中获取随机元素 " + randomNum)

/* 长度扩展 */
nums = myArray.extend(nums, 3)
println("将数组长度扩展至 8 ,得到 nums = " + nums.mkString(" "))

/* 插入元素 */
myArray.insert(nums, 6, 3)
println("在索引 3 处插入数字 6 ,得到 nums = " + nums.mkString(" "))

/* 删除元素 *//* 删除元素 */
myArray.remove(nums, 2)
println("删除索引 2 处的元素,得到 nums = " + nums.mkString(" "))

/* 遍历数组 *//* 遍历数组 */
myArray.traverse(nums)

/* 查找元素 *//* 查找元素 */
val index = myArray.find(nums, 3)
println("在 nums 中查找元素 3 ,得到索引 = " + index)
}

}
96 changes: 96 additions & 0 deletions codes/scala/chapter_array_and_linkedlist/linked_list.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

/**
* File: linked_list.scala
* Author: WangBlue (wangjiping596@gmail.com)
* Create: 2024-08-10 11:37
*/
class linked_list {

/* 在链表的节点 n0 之后插入节点 P */
def insert(n0: ListNode, P: ListNode): Unit = {
val n1: ListNode = n0.next
n0.next = P
P.next = n1
}

/* 删除链表的节点 n0 之后的首个节点 */
def remove(n0: ListNode): Unit = {
if (n0.next == null) {
return;
}
// n0 -> P -> n1
val P: ListNode = n0.next
val n1: ListNode = P.next
n0.next = n1

}

/* 访问链表中索引为 index 的节点 */
def access(head: ListNode, index: Int): ListNode = {
var temp: ListNode = head
for (i <- 0 until index) {
if (temp == null) {
return null
}
temp = temp.next
}
temp
}

/* 在链表中查找值为 target 的首个节点 */
def find(head: ListNode, target: Int): Int = {
var temp: ListNode = head
var index: Int = 0
while (temp != null) {
if (temp.value == target) {
return index
}
temp = temp.next
index += 1
}
-1
}

}


/* Driver Code */
object linked_list {
def main(args: Array[String]): Unit = {
/* 初始化链表 */
// 初始化各个节点
val node0: ListNode = new ListNode(1)
val node1: ListNode = new ListNode(3)
val node2: ListNode = new ListNode(2)
val node3: ListNode = new ListNode(5)
val node4: ListNode = new ListNode(4)

// 构建节点之间的引用
node0.next = node1
node1.next = node2
node2.next = node3
node3.next = node4
println("初始化的链表为")
val printUtil: PrintUtil = new PrintUtil()
printUtil.printLinkedList(node0)

/* 插入节点 */
val myLinkedList: linked_list = new linked_list()
myLinkedList.insert(node0, new ListNode(0))
println("插入节点后的链表为")
printUtil.printLinkedList(node0)

/* 删除节点 */
myLinkedList.remove(node0)
println("删除节点后的链表为")
printUtil.printLinkedList(node0)

/* 访问节点 */
val node: ListNode = myLinkedList.access(node0, 3)
println("链表中索引 3 处的节点的值 = " + node.value)

/* 查找节点 *//* 查找节点 */
val index: Int = myLinkedList.find(node0, 2)
println("链表中值为 2 的节点的索引 = " + index)
}
}
71 changes: 71 additions & 0 deletions codes/scala/chapter_array_and_linkedlist/list.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import java.util.Collections
import scala.collection.mutable.ListBuffer

/**
* File: list.scala
* Author: WangBlue (wangjiping596@gmail.com)
* Create: 2024-08-10 17:52
*/
object list {
def main(args: Array[String]): Unit = {
/* 初始化列表 */
// 注意数组的元素类型是 int[] 的包装类 Integer[]
val numbers: Array[Integer] = Array(1, 3, 2, 5, 4)
// 使用ListBuffer初始化列表,使得nums列表是可变的
val nums: ListBuffer[Integer] = ListBuffer(numbers: _*)
println("列表 nums = " + nums.toString)

/* 访问元素 */
val num = nums(1)
println("访问索引 1 处的元素,得到 num = " + num.toString)

/* 更新元素 *//* 更新元素 */
val updatedNums = nums.updated(1, 0)
println("将索引 1 处的元素更新为 0 ,得到 nums = " + updatedNums.toString)

/* 清空列表 *//* 清空列表 */
nums.clear()
println("清空列表后 nums = " + nums.toString)


/* 在尾部添加元素 */
nums += (1)
nums += (3)
nums += (2)
nums += (5)
nums += (4)
println("添加元素后 nums = " + nums.toString())

/* 在中间插入元素 */
nums.insert(3, 6)
println("在索引 3 处插入数字 6 ,得到 nums = " + nums)

/* 删除元素 */
nums.remove(3)
println("删除索引 3 处的元素,得到 nums = " + nums)

/* 通过索引遍历列表 */
var count: Int = 0
for (i <- 0 until nums.size) {
count += nums(i)
}


/* 直接遍历列表元素 */
for (num <- nums) {
count += num
}

/* 拼接两个列表 */
val nums1: ListBuffer[Integer] = ListBuffer(6, 8, 7, 10, 9)
nums ++= nums1
println("将列表 nums1 拼接到 nums 之后,得到 nums = " + nums)

/* 排序列表 *//* 排序列表 */
val numsSorted: ListBuffer[Integer] = nums.sorted
println("排序列表后 nums = " + numsSorted.toString())


}

}
Loading