对数组和字符串操作

编写一个方法可以生成随机的字符串数组,字符串由数字和字母组成,参数如下:

  • arrySize:数组长度
  • strSize: 字符串长度
public static void main(String[] args) throws InterruptedException {
        String[] randomStringArry = generateStringArry(10,10);
        System.out.println("Arrays.toString(randomStringArry) = " + Arrays.toString(randomStringArry));
    }

    private static String[] generateStringArry(int arrySize, int strSize ) {
        String[] randomStringArry= new String[arrySize];
        String chars="abcdefghijklmnopqrstuvwsyz0123456789";
        Random random=new Random();
        for (int i = 0; i 

编写一个方法去除字符串的连续重复单词(不区分大小写),比如aaaabbb去除后ab,aaAabb去除后ab

  public static void main(String[] args) throws InterruptedException {
        String str1 = "aaaabbb";
        String str2 = "aaABbb";

        String result1 = removeConsecutiveDuplicates(str1);
        String result2 = removeConsecutiveDuplicates(str2);

        System.out.println(result1);  // 输出: ab
        System.out.println(result2);  // 输出: ab
    }
    public static String removeConsecutiveDuplicates(String str) {
        StringBuilder result = new StringBuilder();
        int length = str.length();

        // 遍历字符串中的每个字符
        for (int i = 0; i < length; i++) {
            char currentChar = str.charAt(i);
            result.append(currentChar);

            // 跳过连续重复的字符
            while (i + 1 < length && Character.toLowerCase(currentChar) == Character.toLowerCase(str.charAt(i + 1))) {
                i++;
            }
        }

        return result.toString();
    }

你可能感兴趣的:(笔记题目,java,算法,数据结构)