Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use List of Java data structure

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use the List of Java data structures. I hope you will get something after reading this article. Let's discuss it together.

Generics what is generics

Generics: that is, through parameterized types to manipulate multiple data types on the same code. Generics were introduced in Category 2.0. Genericity literally means that it can be manipulated on a variety of data types, somewhat similar to a template.

Advantages: both generic classes and generic methods are reusable, type safe, and efficient, which non-generic classes and non-generic methods cannot have. Generics are commonly used with collections and methods that act on collections.

Classification of generics

Generic class generic method

A simple demonstration of the definition of generics

1. Angle brackets are the sign of generics

2. E is a type variable (Type Variable), and the variable name is generally capitalized

3. E is defined as a formal parameter, which means the type finally passed in by MyArrayList, but it is not known yet.

Public class MyArrayList {private E [] array; private int size;.} the period of action and the simple principles behind generics

Generics is a mechanism that acts during compilation, that is, there is no concept of generics at run time.

During the run time, generic code is what we mentioned above, using Object to achieve the effect (it is not very accurate here, I will write a blog about generics later).

< T >

Represents that the current class is a generic class.

New T [10]; an array T [] t = new T [] that cannot new generic types

The meaning of generics: ① can automatically check types when storing elements. ② can automatically convert types when getting elements.

Parameters of a generic type: cannot be a simple type

Parameters of generic types that do not participate in the composition of types

Interview questions:

How exactly are generics compiled?

1. Generics only work when compiling. At run time, there is no concept of generics!

2. Erase mechanism-> Object-> not rigorous-> We can give an erase boundary

The use of generic classes / / defines an element that is referenced by the Book class MyArrayList MyArrayList books = new MyArrayList (); books.add (new Book ()); / / causes a compilation error that the Person type cannot be converted to the Book type books.add (new Person ()); / / does not need to do a type conversion Book book = book.get (0); / / generates a compilation error that the Book type cannot be converted to the Person type Person person = book.get (0)

From the above code, we can see one way to use a generic class: it only needs to be followed by angle brackets after all types, and the angle brackets artificially define the type that needs to be passed in, that is, E can be seen as the last type.

Note:

Book can only be thought of as the type of E, but in fact the type of E is still Object.

Generics in Java is just a compile-time concept, and at run time, all generics information is eliminated, which is called generics erasure.

Generics summary

Generics are introduced to solve the generality of some containers, algorithms, and other code, and can do type checking during compilation. If you use the Object class, the compiler will not report an error when illegal parameters are passed in.

Generics take advantage of the fact that Object is the ancestor of all classes, and references to the parent class can point to a specific subclass object.

Generics are a compile-time mechanism, that is, MyArrayList and MyArrayList are the same type at run time.

Generics is a legal syntax in java, marked by angle brackets.

< >

Packaging class

Object references can point to any type of object, but there are exceptions. Eight basic data types are not objects, so isn't the generics mechanism about to fail?

In fact, this is true. In order to solve this problem, java introduces a special class, that is, the wrapper class of these eight basic data types, which wraps values such as int into an object during use.

Direct correspondence between basic data types and wrapper classes

Basically the first letter of a type is capitalized, except for Integer and Character.

Use of wrapper classes, boxing (boxing) and unpacking (unboxing)

There are manual packing and automatic packing, and so is unpacking.

You can see that boxing and unpacking brings a lot of code during use, so in order to reduce the burden on developers, java provides an automatic mechanism.

Note: automatic boxing and automatic unpacking is a mechanism that works during compilation

Common methods of using List in List

Use the example import java.util.List;import java.util.ArrayList;import java.util.LinkedList;public class ListDemo {public static void main (String [] args) {List courses = new ArrayList (); courses.add ("Kobe"); courses.add ("Jordan"); courses.add ("Westbrook"); courses.add ("Durant") / / as with arrays, it is allowed to add repeating elements courses.add ("Kobe"); / / print System.out.println (courses) in the order in which they are added; / / access System.out.println (courses.get (0)) in a way similar to array subscript; / / set a new element courses.set (0, "Jordan") to the target location System.out.println (courses); / / Intercept [1,3) Note here is the left opening and right closing interval List subCourses = courses.subList (1,3); System.out.println (subCourses); / / restructuring List courses2 = new ArrayList (courses); System.out.println (courses2); List courses3 = new LinkedList (courses) System.out.println (courses3); / / referenced conversion ArrayList courses4 = (ArrayList) courses2; System.out.println (courses4); / / LinkedList c = (LinkedList) course2; error type LinkedList courses5 = (LinkedList) courses3; System.out.println (courses5); / / ArrayList c = (ArrayList) course3; error type}}

The running results are as follows:

Automatic licensing case

Divided into three java files

Import java.util.ArrayList;import java.util.List;public class TestDemo {public static void main (String [] args) {List deck = CardDemo.buyDeck (); System.out.println ("bought new cards"); System.out.println (deck); System.out.println ("= ="); CardDemo.shuffle (deck); System.out.println ("shuffled cards") System.out.println (deck); System.out.println ("= ="); / / three people, each person takes turns to draw cards, one person has five cards List hands = new ArrayList (); / / thinking hands.add of two-dimensional array (new ArrayList ()); / / add one person hands.add (new ArrayList ()); / / add one person hands.add (new ArrayList ()) / / add one more person, a total of three people for (int I = 0; I < 5; iTunes +) {for (int j = 0; j < 3; Jake +) {hands.get (j) .add (deck.remove (0)) / / the remove here returns the removed elements in the order table, just one card missing from the stack}} System.out.println ("remaining cards"); System.out.println (deck); System.out.println ("cards in A's hand"); System.out.println (hands.get (0)) System.out.println ("cards in B's hand"); System.out.println (hands.get (1)); System.out.println ("cards in C's hand"); System.out.println (hands.get (2));}} public class Card {private int rank;// card value private String suit;// color public Card (int rank, String suit) {this.rank = rank This.suit = suit; @ Override public String toString () {return String.format ("[% s% d]", suit, rank);}} import java.util.ArrayList;import java.util.List;import java.util.Random;public class CardDemo {private static final String [] suits = {"♥", "♠", "♦", "♣"} / / Buy a deck of cards public static List buyDeck () {List deck = new ArrayList (52); for (int I = 0; I < 4; iTunes +) {for (int j = 1; j 0; iMui -) {int r = rand.nextInt (I); / / generate a random positive integer swap (deck, I, r) with 0 roomi }} after reading this article, I believe you have a certain understanding of "how to use List of Java data structures". If you want to know more about it, welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report