JavaSE学习写的一个小的工具类

本文介绍了作者在JavaSE开发中创建的一些工具类,如生成随机数、验证码生成以及使用Maven和JUnit进行测试的示例。内容包括使用`MyUtils`类中的方法,如`createVerifyCode`和`createIntArray`。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

介绍:
在早期写javaseDemo写了一些小的工具类。
在此记录一下。
没有太多技术含量,之后会尝试多写一写。
目前了解到hutool工具有很多集成的好用的工具类。
@since:2022,08.
@Location: ShanXi,China.

Archive 1:

  1. 打印字符串
  2. 生成随机数(之后导入其他好用的ar包就可以)
  3. 生成一个int类型的数组(这个之后可以和Stream流+4个函数式接口进行对内存中数组数据进行处理操作)
  4. 打印一个方便的demo序号(这个之后可以用@Test junit的注解来进行更加方便的展示和分类操作)
    实现的功能:对于很多案例,可以使用maven工程的@Test的junit来作为一个不同种类Demo的代码的提示。
    idea本市自带的收藏bookmark,如果项目去掉idea文件后,就会失效。
  • 效果1:createVerifyCode(int n)
    生成n位的Code验证码。这个之后有很多工具可以自动生成,还有图片,之前尝试的简单的封装。
  • 效果2:
  • Todo://有时间再写了,之前一直用过博客园(显然博客模版,比csdn要好很多,毕竟样式模版可以自己定制的),目前还不会用自建博客的hexo、wordpress,主要难点是:图片的连接问题。一直在考虑要不要弄图床。前端技术,比较拉胯。最近准备找项目写一下。
package com.ygx.java.utils;

import java.util.Arrays;
import java.util.Objects;
import java.util.Random;

/**
 * My Utils class
 *
 * @author tuneinyang
 * @version 1.0
 */
public class MyUtil {

    /**
     * By default, initialize constant
     */
    static {
        separationMarkCount = 0;
    }

    static Random r = new Random();


    //工具类无需创建对象,所以把其构造器私有化
    private MyUtil() {
    }


    /**
     * Due to param n , create n bit of verification code
     *
     * @param n n bit of verification code
     * @return verification code String
     */
    public static String createVerifyCode(int n) {
        String code = "";
        String standard = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        Random r = new Random();
        for (int i = 0; i < n; i++) {
            int index = r.nextInt(n);
            code += standard.charAt(index);
        }
        return code;
    }


    /**
     * return an positive int Array which has amout size of integer Array
     * in the range of 0 to magnitude
     * <p>
     * notice :
     * amount and magnitude is param name
     * <p>
     * example:
     * if you use by that,MyUtil.createIntArray(4,100).
     * this method can do.return new int[]{1,21,55,3};
     *
     * @param amount    Array's size
     * @param magnitude Array's range
     * @return int[] Array
     */
    public static int[] createIntArray(int amount, int magnitude) {

        /**
         * 对数器 (use to compare digital )
         */

        int[] nums = new int[amount];

        for (int i = 0; i < nums.length; i++) {
            nums[i] = r.nextInt(magnitude + 1);
        }

        return nums;
    }


    /**
     * print a init Array .
     * now the array should be int[] type.
     *
     * @param nums a int[] type array
     * @version 1.0
     */
    public static void printInitArray(int[] nums) {
        System.out.print("show init Array :");
        System.out.println(Arrays.toString(nums));
    }


    /**
     * By now, this method is for printing Demo's Separation Mark
     * for a clear show.
     * <p>
     * caution:
     * 1. this method uses a static block to initialize the value.
     * 2. this method is a single thread.
     *
     * @date 20221231
     * @author yangguoxi
     */
    private static int separationMarkCount = 0;

    public static void printSeparationMark() {
        separationMarkCount++;
        System.out.println("==============" + separationMarkCount + "===============");

    }


    /**
     * print your mark for demo show by param you input;
     * <p>
     * for example :
     * (1)
     * <p>
     * ...
     * (2)
     * <p>
     * ...
     * (a)
     * ...
     *
     * @param rank marks you like
     */
    public static void printSeparationMark(Object rank) {
        //subclass's toString method will be use .
        if (Objects.isNull(rank)) {
            return;
        }

        System.out.println("(" + rank + ")");
        System.out.println();

    }


    /**
     * if you input an int number, this method can return the int digital array.
     * <p>
     * example:
     * if you input an int value ,such as 153
     * this method can do.return new int[]{3,5,1};
     * <p>
     * In the array, starting from the lower subscript,
     * the numeric bits of the number are stored in order from the lower to the higher subscript
     *
     * @param inputNumber  any int number you want
     * @return int array that storage into this array
     * @author yangguoxi
     * @date before 20230301
     */

    public static int[] getIntNumberArray(int inputNumber) {
        /**
         * procedures:
         *
         * - 1. get the digit size of the number.
         * - 2. storage every digit to the int array.
         *
         *
         *
         *
         *
         * ps:
         *  this achievement way is a very Brute force algorithm to advance this requirement.
         *  so, the first step is to get how much this int digit has.
         *  the next step is to get every digit from this int param number.
         */
        int digit = 0;          // stroage the lower digit
        int arraySize = 0;
        int[] intDigitalArray = null;
        int number = inputNumber;

        // - 1. get the digit size of the number.
        while (number > 0) {
            digit = number % 10;
            System.out.println(digit);
            number = number / 10;
            arraySize++;
        }


        //- 2. storage every digit to the int array.
        number = inputNumber;
        intDigitalArray = new int[arraySize];

        for (int index = 0; number > 0; index++) {
            digit = number % 10;
            number = number / 10;

            intDigitalArray[index] = digit;

        }

        return intDigitalArray;

    }


}


//ps:
//  预加载static代码块的实现???

Archive2: 全路径打印



import java.util.ArrayList;

/**
 *
 * Descriptions:
 *  这里主要想写一下,全路径的java纯语言的操作等这些的使用操作,应为感觉在类里面运行可能会重用这个操作
 * @author:yangguoxi
 * @date: 2023/3/21
 */
public class GeneralOperations {

	/**使用示例:
		    public static void main(String[] args) {
				 ArrayList list = new ArrayList();
		        System.out.println(getClass(list));
		    }
    
     *   the name of the class of which the object is an instance
     *   @return like getClass()
     */
    public static String getClass(Object o){
        return o.getClass().getName() ;
    }

    /**
     * The simple name of this Class
     * @retuen like getClass().getSimpleName()
     */
    public static String getSimpleClassName(Object o){
        return o.getClass().getSimpleName() ;
    }






}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值