LinkedList

object Test02 {

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

    val resultList = scala.collection.mutable.LinkedList(1,2,3,4,5)   //这里的val resultList 是不可变的 但是里面的元素可变,
    // 使用循环语句,将每个元素的值都乘二
    var currentList  = resultList
    while (currentList != Nil ){
      currentList.elem = currentList.elem*2
      currentList = currentList.next
    }
    println("---------")
    resultList.foreach(x=>print(x+"\t"))
    println("+++++++++++")
    currentList.foreach(x=>print(x+"\t"))  //为什么它 没有输出??
    println("===========")

  }
}

object  Test0201 extends App {
  val currentList = scala.collection.mutable.LinkedList(1,2,3,4,5)
 // 这里改成了  currentList 发现还是什么也没输出
 // var currentList  = resultList
  while (currentList != Nil ){
    currentList.elem = currentList.elem*2
    currentList.next
  }
  println("---------")
  //resultList.foreach(println)
  println("+++++++++++")
  currentList.foreach(println) //
  println("===========")
}

object  Test0202 extends App {
  val currentList = scala.collection.mutable.LinkedList(1,2,3,4,5)

  // var currentList  = resultList
  while (currentList != Nil ){
   println("---------" + currentList)
     currentList.next
    println("---------"+ currentList)   //这里发先他变成了死循环
  }
  println("---------")
 // resultList.foreach(println)
  println("+++++++++++")
  currentList.foreach(println)
  println("===========")

}
//Nil是一个空List,定义为List[Nothing],所有Nil是所有List[T]的子类

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

  val resultList = scala.collection.mutable.LinkedList(1,2,3,4,5)

  var currentList  = resultList
  while (currentList != Nil ){  //Nil是一个空List
    println("==  " + currentList)
  currentList.elem = currentList.elem*2   elem代表获取集合中第一个元素
  currentList = currentList.next  //next表示获取除了第一个元素以外其它的元素
    println(currentList)  // ------currentList 最后变成了 空的 LinkedList(),所以遍历的时候没有输出
}
  println("---------")
  resultList.foreach(x=>print(x+"\t"))
  println()
  println("+++++++++++")
  currentList.foreach(x=>print(x+"\t"))
  println(currentList)
  println("===========")

}
}
//==  LinkedList(1, 2, 3, 4, 5)
//LinkedList(2, 3, 4, 5)
//==  LinkedList(2, 3, 4, 5)
//LinkedList(3, 4, 5)
//==  LinkedList(3, 4, 5)
//LinkedList(4, 5)
//==  LinkedList(4, 5)
//LinkedList(5)
//==  LinkedList(5)
//LinkedList()
//---------
//2	4	6	8	10
//+++++++++++
//===========

因为 .next 表示每次获取 除了第一个元素以外的其他元素,所以currentList最后变成了空的LinkedList() ,所以用 foreach 遍历为空 可以用 println(currentList) 发现输出了 LinkedList() ,, 个人觉得和垃圾回收机制无关 currentList 还是存在的 只是变成了 空的集合

LinkedList是Java中的一个类,它实现了List接口和Deque接口,可以被看作是一个顺序容器、队列和栈。LinkedList的遍历过程和查找过程类似,可以从头节点开始往后遍历。然而,LinkedList不擅长随机位置访问,如果使用随机访问遍历LinkedList,效率会很低。通常情况下,我们会使用foreach循环来遍历LinkedList,因为foreach最终会转换成迭代器形式。LinkedList的遍历核心就是它的迭代器实现。[1] LinkedList的继承体系较为复杂,它继承自AbstractSequentialList类,并实现了List和Deque接口。AbstractSequentialList是一个基于顺序访问的接口,通过继承此类,子类只需实现部分代码即可拥有完整的一套访问某种序列表的接口。LinkedList还实现了Deque接口,Deque又继承自Queue接口,因此LinkedList具备了队列的功能。[2][3] LinkedList的实现方式决定了所有与下标有关的操作都是线性时间复杂度,而在首段或末尾删除元素只需要常数时间复杂度。LinkedList没有实现同步(synchronized),如果需要多个线程并发访问,可以使用Collections.synchronizedList()方法对其进行包装。[2] 总结来说,LinkedList是一个灵活的数据结构,可以用作顺序容器、队列和栈。它的遍历过程需要注意效率问题,不适合随机位置访问。LinkedList的继承体系较为复杂,继承自AbstractSequentialList类,并实现了List和Deque接口。LinkedList的实现方式决定了与下标有关的操作是线性时间复杂度,而在首段或末尾删除元素只需要常数时间复杂度。[1][2][3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值