java常用算法

String 类

String.length(): 返回字符串的长度。
String.substring(): 返回字符串的子字符串。
String.toUpperCase(): 将字符串转换为大写。
String.toLowerCase(): 将字符串转换为小写。
String.trim(): 去除字符串的前导和尾随空格。
String.equals(): 比较两个字符串是否相等。
String.contains(): 判断一个字符串是否包含另一个字符串。
示例:
String str = "Hello, World!";
int length = str.length(); // 输出: 13
String sub = str.substring(0, 5); // 输出: "Hello"
String upper = str.toUpperCase(); // 输出: "HELLO, WORLD!"
String lower = str.toLowerCase(); // 输出: "hello, world!"
String trim = str.trim(); // 如果str前后有空白字符,会去掉;否则输出原字符串
Boolean cont = str.contains("Hello1"); // 输出: "false"

Math 类

Math.abs(): 返回数字的绝对值。
Math.ceil(): 返回大于或等于一个给定数字的最小整数。
Math.floor(): 返回小于或等于一个给定数字的最大整数。
Math.max(): 返回两个数字中的最大值。
Math.min(): 返回两个数字中的最小值。
Math.random(): 返回一个伪随机数。
Math.round(): 四舍五入到最接近的整数。
示例:
int abs = Math.abs(-10); // 输出: 10
double ceil = Math.ceil(3.14); // 输出: 4.0
double floor= Math.floor(3.14); // 输出: 3.0
int max = Math.max(10, 20); // 输出: 20
int min = Math.min(10, 20); // 输出: 10
double random = Math.random(); // 输出: 一个0.0到1.0之间的随机数
double round = Math.round(5.4); // 输出5.0

Arrays 类

Arrays.sort(): 对数组进行排序。
Arrays.binarySearch(): 在已排序的数组中搜索指定元素。
Arrays.asList(): 将数组转换为列表。
示例:
Integer[] arr = {5, 2, 9, 1, 5, 6};
Arrays.sort(arr); // 排序
Arrays.sort(array, Comparator.reverseOrder()); // 倒序
int searchIndex = Arrays.binarySearch(arr, 5); // 搜索元素5在数组中的位置
List<Integer> list = Arrays.asList(arr); // 将数组转换为列表

Collections 类

Collections.sort(): 对列表进行排序。
Collections.binarySearch(): 在已排序的列表中搜索指定元素。
Collections.shuffle(): 对列表进行随机排序。
示例:        
List<String> list = Arrays.asList("Apple", "Banana", "Cherry");
Collections.sort(list); // 正序
Collections.sort(list,Comparator.reverseOrder()); // 反序 (Comparator是Java中的一个接口,用于定义对象之间的排序规则;也可以用匿名函数代替)
list.sort(null); // 正序
list.sort(Comparator.reverseOrder()); // 反序

int index=Collections.binarySearch(list,"Apple"); // 返回下标
Collections.shuffle(list); //  对列表进行随机排序

File 类

File.exists(): 检查文件或目录是否存在。
File.isFile(): 判断是否为文件。
File.isDirectory(): 判断是否为目录。
File.createNewFile(): 创建新文件。
File.delete(): 删除文件或目录。
示例:
String filePath = "example.txt";
File file = new File(filePath);
boolean exists=file.exists();
boolean isFile=file.isFile();
boolean delete=file.delete();
boolean create=file.createNewFile();

 

1、数组拼接和截取:System.arrayCopy

System.arrayCopy的源代码声明 :
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
代码解释:
  Object src : 原数组
  int srcPos : 原数组的起始位置开始
  Object dest : 目标数组
  int destPos : 目标数组的起始位置
  int length : 拷贝原数组的长度
import java.util.Arrays;

public class test2 {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4};
        new test2().rotate(a,3);
    }

    public void rotate(int[] nums, int k) {
        // 向右旋转数组解法,k是移动位数
        int l = nums.length;
        // 新建c数组,拼接2段nums数组
        int[] c = new int[l * 2];
        System.arraycopy(nums, 0, c, 0, l);
        System.arraycopy(nums, 0, c, l, l);
        // k取余,然后nums从c的索引(l-y)开始取数据,长度是l
        int y = k % l;
        System.out.println("拼接完成的数组c  "+Arrays.toString(c));
        System.arraycopy(c, l - y, nums, 0, l);
        System.out.println("nums截取c  "+Arrays.toString(nums));
    }
}
View Code

 2、去重

基本数据类型的数组不能通过Arrays.asList(a) 的方式转化为List,

但可以通过Arrays.stream(new int[]{1, 2, 3, 1}).boxed().collect(Collectors.toList())的方式转化为List,

解释可以参考: https://zhuanlan.zhihu.com/p/67224831

    public static void main(String[] args) {
        //方法一,int类型的数组不能用该方法
        String[] staffs = new String[]{"Tom", "Bob", "Jane"};
        Set<String> staffsSet = new HashSet<>(Arrays.asList(staffs));
        System.out.println("======" + staffsSet);

        Integer[] a = new Integer[]{1, 2, 3, 1};
        HashSet<Integer> b = new HashSet<>(Arrays.asList(a));
        System.out.println("======" + b);

        //方法二
        //boxed的作用就是将int类型的stream转成了Integer类型的Stream
        List<Integer> n = Arrays.stream(new int[]{1, 2, 3, 1})
                .distinct()
                .boxed()
                .collect(Collectors.toList());
        System.out.println("======" + n);
    }
View Code

 

posted @ 2021-12-20 11:16  whitewall  阅读(51)  评论(0)    收藏  举报