本文共 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 i = new Integer();int intValue = i.intValue();
int i = 20;Integer integer = Integer.valueOf(i);
int i = 30;String value = Integer.valueOf(i);
Integer i = Integer.valueOf("3");
int i = Integer.parseInt("134");
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"
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 = 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;}
使用泛型
Pointp1 = new Point ();Object x1 = p1.getX(); // 获取 String 类型的值Point p2 = new Point ();Integer x2 = p2.getX(); // 获取 Integer 类型的值
// 只能存储 String 类型的集合Listlist1 = new ArrayList ();list1.add("A");list1.add("B");// 只能存储 Integer 类型的集合List list2 = new ArrayList ();list2.add(11);list2.add(22);
以上是对工具类、包装类、字符串操作、常用类以及数组等内容的详细介绍,涵盖了常见的开发场景和实用技巧,适合开发者日常工作参考。
转载地址:http://hzizz.baihongyu.com/