Skip to content

06terlan/Printer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java Print Utility Library

Welcome to the Java Printer Library! This library provides a simple and flexible way to print any data type in Java. Whether you're dealing with primitive types, complex objects, or custom data structures like binary trees, this library makes it easy to display data in a readable format.

Table of Contents

Features

  • Print int/Integer: Support for printing both int and Integer types. See the example here
(value: 1234)
  • Print int[]/int[][] array: The ArrayPrinter class is designed to print arrays of integers in a formatted string. It supports both one-dimensional and two-dimensional arrays, making it versatile for various use cases. The output is formatted according to a specified format string defined in the PrinterType interface. See the example here
(value: [1, 2, 3, 4, 5])
(value: [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  • Print TreeNode: Support for printing TreeNode in a vertical format. See the example here
         1
   ┌─────┴─────┐
   2           2
┌──┘
3
  • Helper Classes: Includes utility classes like TreeNode and PrefixTreeNode for working with binary and prefix trees. See the example of TreeNode here and PrefixTreeNode here

Examples

Print int Array Example

// Example for one-dimensional array
int[] values = {1, 2, 3, 4, 5};
ArrayPrinter printer1D = new ArrayPrinter(values);
printer1D.print();  // Output: (value: [1, 2, 3, 4, 5])

// Example for two-dimensional array
int[][] values2D = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};
ArrayPrinter printer2D = new ArrayPrinter(values2D);
printer2D.print();  // Output: (value: [[1, 2, 3], [4, 5, 6], [7, 8, 9]])

Print int and Integer Example

public static void main(String[] args) {
    int intValue = 42;
    
    Printer.prettyPrint(intValue);
    Printer.prettyPrint(Integer.valueOf(intValue));
}

Output:

(value: 42)
(value: 42)

Print TreeNode Example

TreeNode root = new TreeNode(1, new TreeNode(2, new TreeNode(3)), new TreeNode(2));

Printer.prettyPrint(root);

Output:

         1
   ┌─────┴─────┐
   2           2
┌──┘
3

TreeNode Class Example

The TreeNode class is a simple helper class for creating and managing binary trees. Each TreeNode object has an integer value, and references to its left and right children.

TreeNode root = new TreeNode(10);
root.left = new TreeNode(5);
root.right = new TreeNode(15);

You can also define it like that.

TreeNode root = new TreeNode(10, new TreeNode(5), new TreeNode(15));

PrefixTreeNode Class Example

The PrefixTreeNode class represents a node in a prefix tree, also known as a Trie. This data structure is particularly useful for efficient retrieval of strings based on their prefixes, making it ideal for applications such as autocomplete, spell checking, and IP routing.

// Create the root node of the Trie
PrefixTreeNode rootNode = new PrefixTreeNode(null);

// Add a word to the Trie
String word = "apple";
PrefixTreeNode currentNode = rootNode;

for (char ch : word.toCharArray()) {
    currentNode.children.putIfAbsent(ch, new PrefixTreeNode(ch));
    currentNode = currentNode.children.get(ch);
}

// The word "apple" is now added to the Trie

Installation

To use this library in your project, you can either clone the repository or include it as a dependency in your build tool.

Supported JDK versions

  • JDK11+

Contributing

Contributions are welcome! If you have suggestions for new features, optimizations, or find a bug, please open an issue or submit a pull request. Make sure your code follows the project's coding standards.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages