博客
关于我
剑指Java-常用类
阅读量:398 次
发布时间:2019-03-05

本文共 4679 字,大约阅读时间需要 15 分钟。

工具类

工具方法的设计

工具方法均使用 public static 修饰,通过调用工具类名直接使用。为了防止用户创建工具类实例,工具类的构造器应私有化。

公共静态方法的示例

ArraysUtils 类

private ArraysUtils() { // 私有化构造器}public static void sort() {    System.out.println("排序方法");}public static void print() {    System.out.println("打印方法");}// 调用示例ArraysUtils.sort();

TsstDemo 类

public class TsstDemo {    public static void main(String[] args) {        ArraysUtils.sort(); // 调用工具类的静态方法    }}

单例模式

单例模式确保一个类在应用中只存在一个实例。常见实现方式有懒汉式和饿汉式。

懒汉式

private static final SingletonUtil instance = new SingletonUtil(); // 在类变量初始化时创建private SingletonUtil() { // 私有化构造器}public static SingletonUtil getInstance() {    return instance;}

饿汉式

private static final SingletonUtil instance = new SingletonUtil();private SingletonUtil() { // 私有化构造器}public static SingletonUtil getInstance() {    return instance;}

包装类

基本类型与包装类对应关系

基本类型 包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Integer 类操作示例

  • 将 Integer 转为 int:
Integer i = new Integer();int intValue = i.intValue();
  • 将 int 转为 Integer:
int i = 20;Integer integer = Integer.valueOf(i);
  • 将 int 转为 String:
int i = 30;String value = Integer.valueOf(i);
  • 将 String 转为 Integer:
Integer i = Integer.valueOf("3");
  • 将 String 转为 int:
int i = Integer.parseInt("134");
  • 将 Integer 转为 String:
String str = Integer.toString(134);

字符串

字符串的基本操作

  • 判断字符串是否为空:
String str = "123";System.out.println(str.isEmpty()); // true
  • 字符串拼接:
String result = "hello" + "world"; // "helloworld"
  • 字符串查找:
String str = "helloworld";System.out.println(str.contains("w")); // true
  • 替换字符:
String str = "helloworld";String newStr = str.replace("w", "x");System.out.println(newStr); // "hexworld"
  • 插入字符:
StringBuilder sb = new StringBuilder("123");sb.insert(1, "456"); // 插入到位置 1,插入字符串 "456"System.out.println(sb.toString()); // "12456"

常用类

Math 类

  • 随机数生成:
Random random = new Random();double num = random.nextDouble(); // 0 到 1 之间的随机小数int intNum = (int) (random.nextDouble() * 100); // 0 到 99 之间的随机整数
  • 数学运算:
System.out.println(Math.max(99, 10)); // 99System.out.println(Math.min(99, 10)); // 10

Random 类

  • 生成随机字母:
Random random = new Random();StringBuilder sb = new StringBuilder();for (int i = 0; i < 5; i++) {    int num = random.nextInt(25) + 65; // 65 到 90之间的字母    char c = (char) num;    sb.append(c);}System.out.println(sb.toString());

数组

冒泡排序

public class Bubble {    public static void main(String[] args) {        int[] nums = new int[10];        for (int i = 0; i < nums.length; i++) {            nums[i] = new Random().nextInt(100);        }        System.out.println(Arrays.toString(nums));                for (int i = 0; i < nums.length - 1; i++) {            for (int j = 0; j < nums.length - 1 - i; j++) {                if (nums[j] > nums[j + 1]) {                    int temp = nums[j];                    nums[j] = nums[j + 1];                    nums[j + 1] = temp;                }            }        }        System.out.println(Arrays.toString(nums));    }}

选择排序

public class SelectSort {    public static void main(String[] args) {        int[] arr = {7, 6, 5, 4, 3};        System.out.println("排序前:" + Arrays.toString(arr));                for (int x = 0; x < arr.length; x++) {            for (int i = x + 1; i < arr.length; i++) {                if (arr[x] > arr[i]) {                    int temp = arr[x];                    arr[x] = arr[i];                    arr[i] = temp;                }            }        }        System.out.println("排序后:" + Arrays.toString(arr));    }}

二分查找

public class Find {    public static int binarySearch(int[] nums, int value) {        int low = 0;        int high = nums.length - 1;        while (low <= high) {            int mid = (low + high) / 2;            int midValue = nums[mid];            if (value > midValue) {                low = mid + 1;            } else if (value < midValue) {                high = mid - 1;            } else {                return mid;            }        }        return -1;    }    public static void main(String[] args) {        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};        int index = Find.binarySearch(arr, 8);        System.out.println(index);    }}

泛型

泛型的定义与使用

定义泛型

public class Point
{ private T x; private T y;}

使用泛型

Point
p1 = new Point
();Object x1 = p1.getX(); // 获取 String 类型的值Point
p2 = new Point
();Integer x2 = p2.getX(); // 获取 Integer 类型的值

在集合中使用泛型

// 只能存储 String 类型的集合List
list1 = new ArrayList
();list1.add("A");list1.add("B");// 只能存储 Integer 类型的集合List
list2 = new ArrayList
();list2.add(11);list2.add(22);

总结

以上是对工具类、包装类、字符串操作、常用类以及数组等内容的详细介绍,涵盖了常见的开发场景和实用技巧,适合开发者日常工作参考。

转载地址:http://hzizz.baihongyu.com/

你可能感兴趣的文章
mysql-递归查询(二)
查看>>
MySQL5.1安装
查看>>
mysql5.5和5.6版本间的坑
查看>>
mysql5.5最简安装教程
查看>>
mysql5.6 TIME,DATETIME,TIMESTAMP
查看>>
mysql5.6.21重置数据库的root密码
查看>>
Mysql5.6主从复制-基于binlog
查看>>
MySQL5.6忘记root密码(win平台)
查看>>
MySQL5.6的Linux安装shell脚本之二进制安装(一)
查看>>
MySQL5.6的zip包安装教程
查看>>
mysql5.7 for windows_MySQL 5.7 for Windows 解压缩版配置安装
查看>>
Webpack 基本环境搭建
查看>>
mysql5.7 安装版 表不能输入汉字解决方案
查看>>
MySQL5.7.18主从复制搭建(一主一从)
查看>>
MySQL5.7.19-win64安装启动
查看>>
mysql5.7.19安装图解_mysql5.7.19 winx64解压缩版安装配置教程
查看>>
MySQL5.7.37windows解压版的安装使用
查看>>
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>