In this session, we discuss Java ArrayList and LinkedList and then learn about Generics.

本章节,我们学习动态数组,链表和泛型。

ArrayList and LinkedList

动态数组和链表

The ArrayList class and the LinkedList class are two concrete implementations of the List interface.

动态数组类和链表类是列表接口的两种实现方式。

ArrayList stores elements in an array.

动态数组把元素存储在数组中。

The array is dynamically created.

If the capacity of the array is exceeded, a larger new array is created and all the elements from the current array are copied to the new array.

LinkedList stores elements in a linked list.

Which of the two classes you use depends on your specific needs.

If you need to support random access through an index without inserting or removing elements at the beginning of the list, Array-List is the most efficient.

If, however, your application requires the insertion or deletion of elements at the beginning of the list, you should choose LinkedList. A list can grow or shrink dynamically.

Once it is created, an array is fixed.

If your application does not require the insertion or deletion of elements, an array is the most efficient data structure.

java.util.ArrayList

导入类

ArrayList is a resizable-array implementation of the List interface.

ArrayList是List接口的一种实现形式,它是长度可变的数组。

It also provides methods for manipulating the size of the array used internally to store the list, as shown following.

它也提供了上面这些操作数组长度的方法,可以用来存储列表。

Each ArrayList instance has a capacity, which is the size of the array used to store the elements in the list. It is always at least as large as the list size.

As elements are added to an ArrayList, its capacity grows automatically.

An ArrayList does not automatically shrink. You can use the trimToSize() method to reduce the array capacity to the size of the list.

Slide 5:
LinkedList is a linked list implementation of the List interface. In addition to implementing the List interface, this class provides the methods for retrieving, inserting, and removing elements from both ends of the list, as shown in this slide.

Example of ArrayList

动态数组举例

This slide shows an example of using Arraylist to create a list of cars and manipulate them.

Slide 7:

This slide shows an example of using LinkedList to create a list of cars and manipulate them.

Generic Types

泛型

ArrayList and LinkedList are known as generic classes with a generic type E.

You can specify a concrete type to replace E when creating an ArrayList, or LinkedlList.

For example, the following statement creates an ArrayList and assigns its reference to variable cities. This ArrayList object can be used to store strings.

Slide 9:

Generics is the capability to parameterize types. With this capability, you can define a class or a method with generic types that can be substituted using concrete types by the compiler. For example, you may define a generic stack class that stores the elements of a generic type. From this generic class, you may create a stack object for holding strings and a stack object for holding numbers. Here, strings and numbers are concrete types that replace the generic type.

Slide 10:

The data items in any one collection have the same or related—by inheritance—data types. For example, we might have a collection of strings, a collection of Name objects, a collection of Student objects, and so on. Instead of writing a different class for each of these collections, Java enables you to write a placeholder instead of an actual class type within the definition of a class or interface. This is possible because of a feature known as generics. By using generics, you can define a class of objects whose data type is determined later by the client of your class.
Generics enable you to write a placeholder instead of an actual class type within the definition of a class or interface. The placeholder is a generic data type, or simply a generic type or a type parameter. When you define a class whose instances hold various data collections, you need not give a specific data type for the objects in these collections. Instead, by using a generic data type rather than an actual data type, you define a generic class whose client chooses the data type of the objects in the collection.

Slide 11:

A generic type is a generic class or interface that is parameterized over types. The Box class shown in this slide will be modified to demonstrate the concept.
Let's begin by examining a non-generic Box class that operates on the string data type. It needs only to provide two methods: set, which adds a string to the box, and get, which retrieves it:

Slide 12:

A generic class is defined with the syntax shown in this slide: class name<T1, T2, ..., Tn>
The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variables) T1, T2, ..., and Tn.
To update the Box class to use generics, you create a generic type declaration by changing the code "public class Box" to "public class Box<T>". This introduces the type variable, T, that can be used anywhere inside the class.
With this change, the Box class becomes like the one shown in this slide.
As you can see, all occurrences of Object are replaced by T. A type variable can be any non- primitive type you specify: any class type, any interface type, any array type, or even another type variable. This same technique can be applied to create generic interfaces.
 
Slide 13:

To reference the generic Box class from within your code, you must perform a generic type invocation, which replaces T with some concrete value, such as String:
Box<String> strBox;
Like any other variable declaration, this code does not actually create a new Box object. It simply declares that integerBox will hold a reference to a "Box of Integer", which is how Box<Integer> is read.
To instantiate this class, use the new keyword, as usual, but place <String> between the class name and the parenthesis:
Box<String> integerBox = new Box<String>();
You can think of a generic type invocation as being similar to an ordinary method invocation, but instead of passing an argument to a method, you are passing a type argument — String in this case
— to the Box class itself.
An invocation of a generic type is generally known as a parameterized type.

Slide 14:

There are many data structures and algorithms in Java’s libraries that are specifically designed so that they only work with object types (not primitives). To get around this obstacle, Java defines a wrapper class for each base type. An instance of each wrapper type stores a single value of the corresponding base type. Java provides additional support for implicitly converting between base types and their wrapper types through a process known as automatic boxing and unboxing.

Slide 15:

In this slide, we show the base types and their corresponding wrapper class, along with examples of how objects are created and accessed.
In any context for which an Integer is expected (for example, as a parameter), an int value k can be expressed, in which case Java automatically boxes the int, with an implicit call to new Integer(k). In reverse, in any context for which an int is expected, an Integer value v can be given in which case Java automatically unboxes it with an implicit call to v.intValue( ). Similar conversions are made with the other base-type wrappers. Finally, all of the wrapper types provide support for converting back and forth between string literals.

Slide 16:

This slide shows the implenetation of generic linked list.

ArrayList, LinkedList and Generics in Java--链表和泛型相关推荐

  1. java vector arraylist linkedlist用法与区别

    首先,它们是list的实现类,大致说一下vector arraylist linkedlist的区别. 1.线程安全来讲, vector是线程安全,arraylist linkedlist线程不安全. ...

  2. Vector ArrayList Hashtable HashMap ArrayList LinkedList

    1. Vector & ArrayList 1)  Vector的方法都是同步的(Synchronized),是线程安全的(thread-safe),而ArrayList的方法不是,由于线程的 ...

  3. ArrayList,LinkedList,Vector的异同点

    先总结下ArrayList和LinkedList的区别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayLi ...

  4. java 链表 最小堆优先级队列_关于Java集合的小抄

    List ArrayList 以数组实现.节约空间,但数组有容量限制.超出限制时会增加50%容量,用System.arraycopy()复制到新的数组,因此最好能给出数组大小的预估值.默认第一次插入元 ...

  5. java链表实现_链表的原理及java实现

    一:单向链表基本介绍 链表是一种数据结构,和数组同级.比如,Java中我们使用的ArrayList,其实现原理是数组.而LinkedList的实现原理就是链表了.链表在进行循环遍历时效率不高,但是插入 ...

  6. ArrayList, LinkedList, Vector - dudu:史上最详解

    ArrayList, LinkedList, Vector - dudu:史上最详解 我们来比较一下ArrayList, LinkedLIst和Vector它们之间的区别.BZ的JDK版本是1.7.0 ...

  7. Collection,List,ArrayList,LinkedList集合

    Collection集合 是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素 lJDK 不提供此接口的任何直接实现,它提供更具体的子接口(如Set和List)实现 Coll ...

  8. Java基础学习——泛型(generics)学习一

    概述 在JDK 5.0,Java语言引入了好几个新的功能,其中很重要的一个就是泛型(generics). 本文就是对泛型的一个概述.你可以很熟悉其他语言中的类似结构,比如C++里的模板(templat ...

  9. Java源码详解五:ArrayList源码分析--openjdk java 11源码

    文章目录 注释 类的继承与实现 构造函数 add操作 扩容函数 remove函数 subList函数 总结 本系列是Java详解,专栏地址:Java源码分析 ArrayList 官方文档:ArrayL ...

最新文章

  1. LFFD ncnn torch
  2. angular1x初始与架构演进(三)Ui-Router+OcLazyLoad加载模块
  3. STM32(Cortex-M3)启动过程+IAR中xcl及icf文件详解
  4. 计算机房的英语用谐音怎么读,“人机对话”学英语 发音不准就过不了电脑关...
  5. stm32滴答计时器_stm32笔记:Systick系统滴答定时器
  6. python one class svm_sklearn例程:OneClassSVM物种分布建模
  7. 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_01-自定义查询页面-服务端-Dao...
  8. 佳能 2900 linux 驱动下载,在Linux下安装打印驱动,以佳能LBP2900+为例
  9. 软件测试基础知识 + 面试理论(超详细)
  10. ov5640_rgmii_udp
  11. C语言文件指针,如何对文件进行操作,文件指针FILE,指向文件指针
  12. 2022年全球市场GPS追踪装置总体规模、主要生产商、主要地区、产品和应用细分研究报告
  13. SQL Leetcode练习题
  14. php receivemail下载,php receivemail,php mail,preceive
  15. 四级英语口语模拟测试软件,英语四级口语模拟题:非常有用
  16. java BigDecimal.ROUND_UP和BigDecimal.ROUND_DOWN的用处
  17. 2022年mvnrepository跳过人机验证
  18. 一个渣渣对OLSR的简单理解
  19. ie6下z-index不起作用?
  20. 5G新型网络架构关键技术 — 无线mesh与动态自组网络和无线资源调度与共享

热门文章

  1. 榆林市科技馆项目的变电所运维
  2. 十分钟看懂语义分割技术【转载】
  3. EasyRecovery15最可靠的电脑计算机数据恢复软件
  4. 关于jiaminghi 中Datav的数字翻牌器字体失效的解决方案
  5. 互联网日报 | 名创优品潮玩全球首店开业;百度昆仑1芯片实现量产;特斯拉在华超充站突破600座...
  6. 态势感知通常是主客观的混合物
  7. 优雅地处理运行时权限请求
  8. linux下的3A游戏,游知有味:任天堂的游戏都不算?什么才叫3A级大作
  9. 英文RouterOS秒变中文版
  10. TCPDump流量监控工具