Home Java javaTutorial Generics in Java programming ideas implement a stack class Share

Generics in Java programming ideas implement a stack class Share

Jan 24, 2017 pm 03:17 PM

I think the author wrote it so well that I have to save it.

Understanding of this example:

//Type parameters cannot use basic types. T and U are actually the same type.

//Every time new data is placed, it becomes a new top. Push the original top down one level and establish a link through the pointer.

//The end sentinel is a node created by the default constructor that is consistent with end() returning true.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

//: generics/LinkedStack.java

// A stack implemented with an internal linked structure.

package generics;

public class LinkedStack<T> {

  private static class Node<U> {

    U item;

    Node<U> next;

    Node() { item = null; next = null; }

    Node(U item, Node<U> next) {

      this.item = item;

      this.next = next;

    }

    boolean end() { return item == null && next == null; }

  }

  private Node<T> top = new Node<T>(); // End sentinel

  public void push(T item) {

    top = new Node<T>(item, top);

  }   

  public T pop() {

    T result = top.item;

    if(!top.end())

      top = top.next;

    return result;

  }

  public static void main(String[] args) {

    LinkedStack<String> lss = new LinkedStack<String>();

    for(String s : "Phasers on stun!".split(" "))

      lss.push(s);

    String ss;

    while((ss = lss.pop()) != null)

      System.out.println(ss);

      //----- if put integer into the LinkedList

      LinkedStack<Integer> lii = new LinkedStack<Integer>();

      for(Integer i = 0; i < 10; i++){

          lii.push(i);

      }

      Integer end;

      while((end = lii.pop()) != null)

          System.out.println(end);

      //----- integer test end!

  }

 

   

}

/* Output:

stun!

on

Phasers

*/

Copy after login

For more generics in Java programming ideas, implement a stack class. To share related articles, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Mar 07, 2025 pm 06:09 PM

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

How does Java's classloading mechanism work, including different classloaders and their delegation models?

Node.js 20: Key Performance Boosts and New Features Node.js 20: Key Performance Boosts and New Features Mar 07, 2025 pm 06:12 PM

Node.js 20: Key Performance Boosts and New Features

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Mar 07, 2025 pm 05:52 PM

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed

Iceberg: The Future of Data Lake Tables Iceberg: The Future of Data Lake Tables Mar 07, 2025 pm 06:31 PM

Iceberg: The Future of Data Lake Tables

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?

See all articles