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.