Java 打印模板大全

• Java 打印模板

下面这份够覆盖刷题和面试里大多数场景。

import java.util.*;

public class PrintTemplates {

// 链表节点

static class ListNode {

int val;

ListNode next;

ListNode(int val) { this.val = val; }

}

// 二叉树节点

static class TreeNode {

int val;

TreeNode left, right;

TreeNode(int val) { this.val = val; }

}

// 自定义对象

static class User {

String name;

int age;

User(String name, int age) {

this.name = name;

this.age = age;

}

@Override

public String toString() {

return "User{name='" + name + "', age=" + age + "}";

}

}

public static void main(String[] args) {

// 1. 基本类型 / 字符串

int x = 10;

String s = "hello";

System.out.println(x);

System.out.println(s);

// 2. 一维数组

int[] arr = {1, 2, 3, 4};

System.out.println(Arrays.toString(arr));

// 3. 二维数组

int[][] matrix = {{1, 2}, {3, 4}, {5, 6}};

System.out.println(Arrays.deepToString(matrix));

// 4. 一行一行打印二维数组

for (int[] row : matrix) {

System.out.println(Arrays.toString(row));

}

// 5. List

List<Integer> list = Arrays.asList(1, 2, 3);

System.out.println(list);

// 6. Set

Set<String> set = new HashSet<>(Arrays.asList("a", "b", "c"));

System.out.println(set);

// 7. Map

Map<String, Integer> map = new HashMap<>();

map.put("Alice", 18);

map.put("Bob", 20);

System.out.println(map);

// 8. 自定义对象

User user = new User("Tom", 22);

System.out.println(user);

// 9. 对象数组 / 对象集合

User[] users = {new User("A", 10), new User("B", 20)};

System.out.println(Arrays.toString(users));

System.out.println(Arrays.asList(users));

// 10. 链表

ListNode head = new ListNode(1);

head.next = new ListNode(2);

head.next.next = new ListNode(3);

printList(head);

// 11. 二叉树

TreeNode root = new TreeNode(1);

root.left = new TreeNode(2);

root.right = new TreeNode(3);

root.left.right = new TreeNode(4);

System.out.print("前序: ");

preorder(root);

System.out.println();

System.out.print("中序: ");

inorder(root);

System.out.println();

System.out.print("后序: ");

postorder(root);

System.out.println();

System.out.print("层序: ");

levelOrder(root);

System.out.println();

// 12. 格式化输出

System.out.printf("name=%s, age=%d%n", "Jerry", 18);

}

// 打印链表

public static void printList(ListNode head) {

while (head != null) {

System.out.print(head.val);

if (head.next != null) {

System.out.print(" -> ");

}

head = head.next;

}

System.out.println();

}

// 前序遍历

public static void preorder(TreeNode root) {

if (root == null) return;

System.out.print(root.val + " ");

preorder(root.left);

preorder(root.right);

}

// 中序遍历

public static void inorder(TreeNode root) {

if (root == null) return;

inorder(root.left);

System.out.print(root.val + " ");

inorder(root.right);

}

// 后序遍历

public static void postorder(TreeNode root) {

postorder(root.right);

System.out.print(root.val + " ");

}

// 层序遍历

public static void levelOrder(TreeNode root) {

if (root == null) return;

Queue<TreeNode> queue = new LinkedList<>();

queue.offer(root);

while (!queue.isEmpty()) {

TreeNode node = queue.poll();

System.out.print(node.val + " ");

if (node.left != null) queue.offer(node.left);

if (node.right != null) queue.offer(node.right);

}

}

}

最常用速记版

  • 一维数组:Arrays.toString(arr)

  • 二维数组:Arrays.deepToString(arr)

  • 集合:System.out.println(list)

  • Map:System.out.println(map)

  • 链表:while 遍历打印

  • 二叉树:前序 / 中序 / 后序 / 层序

  • 自定义对象:重写 toString()

补充

如果你刷 LeetCode,最实用的是这几个:

System.out.println(Arrays.toString(arr));

System.out.println(Arrays.deepToString(matrix));

System.out.println(list);

System.out.println(map);

printList(head);

levelOrder(root);

相关推荐
AI人工智能+电脑小能手2 分钟前
【大白话说Java面试题】【Java基础篇】第20题:HashMap在计算index的时候,为什么要对数组长度做减1操作
java·开发语言·数据结构·后端·面试·哈希算法·hash-index
凯瑟琳.奥古斯特3 分钟前
Bootstrap快速上手指南
开发语言·前端·css·bootstrap·html
嵌入式×边缘AI:打怪升级日志4 分钟前
嵌入式Linux开发(了解交叉编译工具链的组成)
java·linux·运维
FreeGo~13 分钟前
Linux 系统编程 进程篇 (五)
java·linux·服务器
我就是妖怪16 分钟前
Kimi K2.6 智能效果实测与能力全景展示
开发语言
中二痞19 分钟前
下载Python 版本,环境变量变更以及PyCharm更换python版本
开发语言·python·pycharm
故事和你9121 分钟前
洛谷-算法2-3-分治与倍增5
开发语言·数据结构·c++·算法·动态规划·图论
SilentSamsara22 分钟前
标准库精讲:collections/itertools/functools/pathlib 实战
开发语言·vscode·python·青少年编程·pycharm
逻辑驱动的ken26 分钟前
Java高频面试考点场景题17
开发语言·jvm·面试·求职招聘·春招
charlie11451419127 分钟前
通用GUI编程技术——图形渲染实战(三十九)——纹理与采样器:从WIC加载到GPU渲染
开发语言·c++·图形渲染·win32