当前位置:网站首页>Simple usage of LinkedList (2022.6.13-6.19)
Simple usage of LinkedList (2022.6.13-6.19)
2022-06-30 19:34:00 【Water is water】
package com.reviewDemo;
import java.util.Iterator;
import java.util.LinkedList;
/**
* @Author: Bsx
* @Date: 2022/6/14
* @Description: LinkedList Use Demo
*/
public class Test_002 {
public static void main(String[] srgs) {
// Create and store int Type of linkedList
LinkedList<Integer> linkedList = new LinkedList<>();
/************************** linkedList Basic operation ************************/
linkedList.addFirst(0); // Add elements to the beginning of the list
linkedList.add(1); // Add elements at the end of the list
linkedList.add(2, 2); // Add the element in the specified position
linkedList.addLast(3); // Add elements to the end of the list
System.out.println("LinkedList( Direct output ): " + linkedList);
System.out.println("getFirst() Get the first element : " + linkedList.getFirst()); // Returns the first element of this list
System.out.println("getLast() Get the last element : " + linkedList.getLast()); // Return the last element of this list
System.out.println("removeFirst() Delete the first element and return : " + linkedList.removeFirst()); // Remove and return the first element of this list
System.out.println("removeLast() Delete the last element and return : " + linkedList.removeLast()); // Remove and return the last element of this list
System.out.println("After remove:" + linkedList);
System.out.println("contains() Method to determine whether the list contains 1 This element :" + linkedList.contains(1)); // Determine that this list contains the specified element , If it is , Then return to true
System.out.println(" The linkedList Size : " + linkedList.size()); // Returns the number of elements in this list
/************************** Location access operations ************************/
System.out.println("-----------------------------------------");
linkedList.set(1, 3); // Replace the element at the specified location in this list with the specified element
System.out.println("After set(1, 3):" + linkedList);
System.out.println("get(1) Get the designated location ( Here for 1) The elements of : " + linkedList.get(1)); // Returns the element in this list at the anchor
/************************** Search operation ************************/
System.out.println("-----------------------------------------");
linkedList.add(3);
System.out.println("indexOf(3): " + linkedList.indexOf(3)); // Returns the index of the specified element that first appears in this list
System.out.println("lastIndexOf(3): " + linkedList.lastIndexOf(3));// Returns the index of the last specified element in this list
/************************** Queue operation ************************/
System.out.println("-----------------------------------------");
System.out.println("peek(): " + linkedList.peek()); // Gets but does not remove the header of this list
System.out.println("element(): " + linkedList.element()); // Gets but does not remove the header of this list
linkedList.poll(); // Get and remove the header of this list
System.out.println("After poll():" + linkedList);
linkedList.remove();
System.out.println("After remove():" + linkedList); // Get and remove the header of this list
linkedList.offer(4);
System.out.println("After offer(4):" + linkedList); // Add the specified element to the end of this list
/************************** Deque operation ************************/
System.out.println("-----------------------------------------");
linkedList.offerFirst(2); // Insert the specified element at the beginning of this list
System.out.println("After offerFirst(2):" + linkedList);
linkedList.offerLast(5); // Insert the specified element at the end of this list
System.out.println("After offerLast(5):" + linkedList);
System.out.println("peekFirst(): " + linkedList.peekFirst()); // Gets but does not remove the first element of this list
System.out.println("peekLast(): " + linkedList.peekLast()); // Gets but does not remove the last element of this list
linkedList.pollFirst(); // Get and remove the first element of this list
System.out.println("After pollFirst():" + linkedList);
linkedList.pollLast(); // Get and remove the last element of this list
System.out.println("After pollLast():" + linkedList);
linkedList.push(2); // Push elements onto the stack represented by this list ( Insert into the head of the list )
System.out.println("After push(2):" + linkedList);
linkedList.pop(); // Pop an element from the stack represented by this list ( Get and remove the first element of the list )
System.out.println("After pop():" + linkedList);
linkedList.add(3);
linkedList.removeFirstOccurrence(3); // Remove the first occurrence of the specified element... From this list ( Traversing the list from head to tail )
System.out.println("After removeFirstOccurrence(3):" + linkedList);
linkedList.removeLastOccurrence(3); // Remove the last occurrence of the specified element... From this list ( Traversing the list from tail to head )
System.out.println("After removeFirstOccurrence(3):" + linkedList);
/************************** Traversal operation ************************/
System.out.println("-----------------------------------------");
linkedList.clear();
for (int i = 0; i < 100000; i++) {
linkedList.add(i);
}
// Iterator traversal
long start = System.currentTimeMillis();
Iterator<Integer> iterator = linkedList.iterator();
while (iterator.hasNext()) {
iterator.next();
}
long end = System.currentTimeMillis();
System.out.println("Iterator:" + (end - start) + " ms");
// Sequential traversal ( Random ergodicity )
start = System.currentTimeMillis();
for (int i = 0; i < linkedList.size(); i++) {
linkedList.get(i);
}
end = System.currentTimeMillis();
System.out.println("for:" + (end - start) + " ms");
// Another kind for Loop traversal
start = System.currentTimeMillis();
for (Integer i : linkedList)
;
end = System.currentTimeMillis();
System.out.println("for2:" + (end - start) + " ms");
// adopt pollFirst() or pollLast() To traverse LinkedList
LinkedList<Integer> temp1 = new LinkedList<>();
temp1.addAll(linkedList);
start = System.currentTimeMillis();
while (temp1.size() != 0) {
temp1.pollFirst();
}
end = System.currentTimeMillis();
System.out.println("pollFirst() or pollLast():" + (end - start) + " ms");
// adopt removeFirst() or removeLast() To traverse LinkedList
LinkedList<Integer> temp2 = new LinkedList<>();
temp2.addAll(linkedList);
start = System.currentTimeMillis();
while (temp2.size() != 0) {
temp2.removeFirst();
}
end = System.currentTimeMillis();
System.out.println("removeFirst() or removeLast():" + (end - start) + " ms");
}
}
边栏推荐
- Go language learning tutorial (10)
- 「杂谈」如何改善数据分析工作中的三大被动局面
- com. alibaba. fastjson. Jsonobject tojsonstring eliminate circular reference
- Evolution of screen display technology
- 基于STM32单片机的测温仪
- 20200525 Biotechnology - Sichuan Normal University self taught Biotechnology (undergraduate) examination plan txt
- Temperature measuring instrument based on STM32 single chip microcomputer
- Memory Limit Exceeded
- Pyth Solana is a bridge connecting reality on the chain
- Ansi/ul 94 class 5-V vertical combustion test
猜你喜欢

Construction and practice of full stack code test coverage and use case discovery system

sql连续登录问题

码蹄集 - MT3435 · 赋值 - 二分图问题 - 图文讲解

ArcGIS no plug-in load (no offset) day map

虚拟主机什么时候适合更换成云主机?

German agbb VOC hazardous substances test

Promise from recognition to use

连接实验室服务器

在广州的朋友,有机会可以参加下

嵌入式软件开发新趋势:DevOps
随机推荐
小球大小随机,随机运动碰撞
Some interesting modules
手机炒股开户安全嘛!?
Is it safe to open an account for mobile phone stock trading!?
Ditto设置全局仅粘贴文本快捷键
Evolution of screen display technology
VoIP Push 在海外音视频业务中的应用
CTF flow analysis common questions (II) -usb flow
mysql函数获取全路径
内存数据库如何发挥内存优势?
Introduction to Po mode "suggestions collection"
Promise从认识到使用
JVM FAQs
实现各种效果和功能的按钮,读这篇文章就够了
简述机器学习中的特征工程
MQ优缺点(2022.5.2-5.8)
mysql 递归
mysql中union和union all的区别
Redis beginner to master 01
Whether the SQL is indexed