博客
关于我
剑指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 CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>
mysql csv import meets charset
查看>>
multivariate_normal TypeError: ufunc ‘add‘ output (typecode ‘O‘) could not be coerced to provided……
查看>>
MySQL DBA 数据库优化策略
查看>>
multi_index_container
查看>>
MySQL DBA 进阶知识详解
查看>>
Mura CMS processAsyncObject SQL注入漏洞复现(CVE-2024-32640)
查看>>
Mysql DBA 高级运维学习之路-DQL语句之select知识讲解
查看>>
mysql deadlock found when trying to get lock暴力解决
查看>>
MuseTalk如何生成高质量视频(使用技巧)
查看>>