MengBridgeCourse 中文版

第 11 章 列表与迭代器

| 关于   英文版   «  10.3. 单元 10 实验 1 占位符   ::   目录   ::   11.2. 单元 11 实验 1 占位符  »

11.1. 列表

11.1.1. 概述与目标

完成本模块后,学生将能够:

  • 区分列表与其他抽象数据类型(栈、队列、集合)的性质和使用场景

  • 使用基于数组或链表的方法在 java 中实现列表

  • 考虑各种设计方法及其对应的效率

  • 追踪和调试列表实现

11.1.1.1. 建议阅读:

第 12 章 - 14 章:链表、使用数组实现的链表、通过链接数据实现的链表 摘自 Data Structures and Abstractions with Java, 4th edition by Frank M. Carrano and Timothy Henry

11.1.2. 列表简介 [13:41]

Follow Along and Engage

下载对应视频的幻灯片。观看视频时,在幻灯片上做笔记,并亲自练习绘制图表!

Video Slides 11.1.2.1-ListIntro.pdf

The List Interface

下载并运行和探索(见下文)Java 文件(在 Eclipse 中自行操作)。

ListInterface.java
package list;

/**
* An interface for the ADT list. Entries in a list have positions that begin
* with 0
*
* @author Frank M. Carrano
* @author Timothy M. Henry
* @author maellis1
* @version July 2024
*/
public interface ListInterface<T> {
   /**
   * Adds a new entry to the end of this list. Entries currently in the list
   * are unaffected. The list's size is increased by 1.
   *
   * @param newEntry
   *            The object to be added as a new entry.
   */
   public void add(T newEntry);

   /**
   * Adds a new entry at a specified position within this list. Entries
   * originally at and above the specified position are at the next higher
   * position within the list. The list's size is increased by 1.
   *
   * @param newPosition
   *            An integer that specifies the desired position of the new
   *            entry.
   * @param newEntry
   *            The object to be added as a new entry.
   * @throws IndexOutOfBoundsException
   *             if either newPosition less than 0 or newPosition greater than
   *             the size of the list.
   */
   public void add(int newPosition, T newEntry);

   /**
   * Removes the entry at a given position from this list. Entries originally
   * at positions higher than the given position are at the next lower
   * position within the list, and the list's size is decreased by 1.
   *
   * @param givenPosition
   *            An integer that indicates the position of the entry to be
   *            removed.
   * @return A reference to the removed entry.
   * @throws IndexOutOfBoundsException
   *             if either givenPosition less than 0 or givenPosition greater
   *             than or equal to the size of the list.
   */
   public T remove(int givenPosition);

   /** Removes all entries from this list. */
   public void clear();

   /**
   * Replaces the entry at a given position in this list.
   *
   * @param givenPosition
   *            An integer that indicates the position of the entry to be
   *            replaced.
   * @param newEntry
   *            The object that will replace the entry at the position
   *            givenPosition.
   * @return The original entry that was replaced.
   * @throws IndexOutOfBoundsException
   *             if either givenPosition less than 0 or givenPosition greater
   *             than or equal to the size of the list.
   */
   public T replace(int givenPosition, T newEntry);

   /**
   * Retrieves the entry at a given position in this list.
   *
   * @param givenPosition
   *            An integer that indicates the position of the desired entry.
   * @return A reference to the indicated entry.
   * @throws IndexOutOfBoundsException
   *             if either givenPosition less than 0 or givenPosition greater
   *             than or equal to the size of the list.
   */
   public T getEntry(int givenPositi son);

   /**
   * Retrieves all entries that are in this list in the order in which they
   * occur in the list.
   *
   * @return A newly allocated array of all the entries in the list. If the
   *         list is empty, the returned array is empty.
   */
   public Object[] toArray();

   /**
   * Sees whether this list contains a given entry.
   *
   * @param anEntry
   *            The object that is the desired entry.
   * @return True if the list contains anEntry, or false if not.
   */
   public boolean contains(T anEntry);

   /**
   * Gets the length of this list.
   *
   * @return The integer number of entries currently in the list.
   */
   public int getLength();

   /**
   * Sees whether this list is empty.
   *
   * @return True if the list is empty, or false if not.
   */
   public boolean isEmpty();
} // end ListInterface

11.1.3. 检查点 1

11.1.4. 交互式:链表 Add() 实现 [10:21]

Follow Along and Engage

下载与视频对应的幻灯片。观看视频时,在幻灯片上做笔记,并亲自练习绘制图表!

Video Slides 11.1.3.1-LinkedListAdd.pdf

11.1.5. 检查点 2

11.1.6. 交互式:使用调试器追踪 Add() [13:33]

Follow Along and Engage

下载与视频对应的幻灯片。观看视频时,在幻灯片上做笔记,并亲自练习绘制图表!

Video Slides 11.1.4.1-TraceAddDebugger.pdf

11.1.7. 交互式:链表移除 () [18:09]

Follow Along, Practice and Explore

在 Eclipse 中,使用 项目 > 下载作业... 菜单命令下载名为"ex11.01-LinkedList"的练习项目。

如果需要在下载 Eclipse 项目时回顾说明,请参阅 01.02: Lab: LightBot for Beginners。

Video Slides 11.1.5.1-LinkedListRemove.pdf

11.1.8. 检查点

11.1.9. 编程练习:列表 1

11.1.10. 交互式:链表详情与选项 [10:19]

Follow Along and Engage

下载与视频对应的幻灯片。观看视频时,在幻灯片上做笔记,并亲自练习绘制图表!

Video Slides 11.1.7.1-LinkedListMoreDetails.pdf

11.1.11. 检查点

11.1.12. 交互式:使用数组实现的列表 [15:48]

Follow Along and Engage

下载与视频对应的幻灯片。观看视频时,在幻灯片上做笔记,并亲自练习绘制图表!

Video Slides 11.1.8.1-ArrayListImplementation.pdf

11.1.13. 编程练习:列表 2

   «  10.3. 单元 10 实验 1 占位符   ::   目录   ::   11.2. 单元 11 实验 1 占位符  »

关闭窗口