• Java -【字符串,数组,哈希表】常用操作


    一. 字符串

    1. 创建字符串:
      可以使用双引号或者String类的构造方法创建字符串。
    String str1 = "Hello World";
    String str2 = new String("Hello World");
    
    • 1
    • 2
    1. 连接字符串:
      可以使用加号或者String类的concat()方法连接字符串。
    String str3 = str1 + str2;
    String str4 = str1.concat(str2);
    
    • 1
    • 2
    1. 获取字符串长度:
      可以使用length()方法获取字符串的长度。
    int len = str1.length();
    
    • 1
    1. 获取字符串中的字符:
      可以使用charAt()方法获取字符串中指定位置的字符。
    char c = str1.charAt(0);
    
    • 1
    1. 截取字符串:
      可以使用substring()方法截取字符串。
    String sub = str1.substring(0, 5);
    
    • 1
    1. 替换字符串:
      可以使用replace()方法替换字符串中的字符或子串。
    String newStr = str1.replace("World", "Java");
    
    • 1
    1. 转换字符串大小写:
      可以使用toUpperCase()和toLowerCase()方法将字符串转换为全大写或全小写。
    String upper = str1.toUpperCase();
    String lower = str1.toLowerCase();
    
    • 1
    • 2
    1. 搜索字符串:
      可以使用indexOf()方法查找字符串中指定字符或子串的位置。
    int index = str1.indexOf('o');
    int index2 = str1.indexOf("World");
    
    • 1
    • 2
    1. 比较字符串:
      可以使用equals()方法比较字符串是否相等。
    boolean isEqual = str1.equals(str2);
    
    • 1
    1. 格式化字符串:
      可以使用String.format()方法将数据格式化为字符串。
    String name = "小明";
    int age = 20;
    String info = String.format("姓名:%s,年龄:%d", name, age);
    
    • 1
    • 2
    • 3
    1. 遍历字符串
    String str = "Hello world!";
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        System.out.println(c);
    }
    
    String str = "Hello world!";
    for (char c : str.toCharArray()) {
        System.out.println(c);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    二. 数组 - 列表

    1. 创建数组:
      可以使用初始化语句或new关键字创建数组。
    int[] a = {1, 2, 3};
    int[] b = new int[3];
    
    • 1
    • 2
    1. 获取数组长度:
      可以使用length属性获取数组长度。
    int len = a.length;
    
    • 1
    1. 访问数组元素:
      可以使用下标操作符[]访问数组元素。
    int element = a[0];
    
    • 1
    1. 修改数组元素:
      可以通过下标操作符[]修改数组元素。
    a[1] = 4;
    
    • 1
    1. 遍历数组:
      可以使用for循环或者foreach语句遍历数组。
    for (int i = 0; i < a.length; i++) {
        System.out.println(a[i]);
    }
    
    for (int element : a) {
        System.out.println(element);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1. 数组复制:
      可以使用Arrays类的copyOf()方法或System.arraycopy()方法复制数组。
    int[] c = Arrays.copyOf(a, 3);
    int[] d = new int[3];
    System.arraycopy(a, 0, d, 0, 3);
    
    • 1
    • 2
    • 3
    1. 反转列表:
      可以使用Collections类的reverse()方法对列表进行反转。
    Collections.reverse(a);
    
    • 1
    1. 数组排序:
      可以使用Arrays类的sort()方法对数组进行排序。
    // 正序
    Arrays.sort(a);
    
    // 倒序,可以先正序再反转
    Collections.reverse(a); 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 数组查找:
      可以使用Arrays类的binarySearch()方法对有序数组进行查找。
    String str = Arrays.toString(a);
    
    • 1
    1. 添加元素
      通过调用add()方法可以将元素添加到ArrayList的末尾
    ArrayList<Integer> list = new ArrayList<>();
    list.add(1); // 添加新元素1
    list.add(2); // 添加新元素2
    
    • 1
    • 2
    • 3
    1. 在指定位置插入元素
      通过调用add(index, element)方法可以在指定位置插入元素。示例代码:
    ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3));
    list.add(1, 4); // 在位置1插入新元素4
    
    • 1
    • 2

    三. 哈希表HashMap - 字典

    1. 创建哈希表
    HashMap<String, Integer> hashMap = new HashMap<>();
    
    • 1
    1. 插入元素
    hashMap.put("apple", 5);
    hashMap.put("banana", 6);
    
    • 1
    • 2
    1. 获取元素
    Integer appleValue = hashMap.get("apple"); // 返回5
    
    • 1
    1. 删除元素
    hashMap.remove("apple");
    
    • 1
    1. 获取哈希表大小
    int size = hashMap.size();
    
    • 1
    1. 判断哈希表是否为空
    boolean isEmpty = hashMap.isEmpty();
    
    • 1
    1. 判断哈希表是否包含某个键或值
    boolean containsKey = hashMap.containsKey("apple");
    boolean containsValue = hashMap.containsValue(6);
    
    • 1
    • 2
    1. 清空哈希表
    hashMap.clear();
    
    • 1
    1. 遍历哈希表
    for (String key : hashMap.keySet()) {
        Integer value = hashMap.get(key);
        System.out.println(key + ": " + value);
    }
    
    • 1
    • 2
    • 3
    • 4
    1. 排序
    HashMap<Integer, Integer> hashMap = new HashMap<>();
    hashMap.put(3, 30);
    hashMap.put(1, 10);
    hashMap.put(2, 20);
    
    List<Map.Entry<Integer, Integer>> list = new ArrayList<>(hashMap.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
        public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
            return o1.getValue().compareTo(o2.getValue());
        }
    });
    
    for (Map.Entry<Integer, Integer> entry : list) {
        System.out.println(entry.getKey() + ": " + entry.getValue());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    这里我们先将哈希表中的元素转换为一个 List,然后通过 Collections.sort() 方法对 List 进行排序,具体排序方式通过传入 Comparator 实现。最后,遍历排好序的 List 输出结果即可。

    以上就是关于Java -【字符串,数组,哈希表】常用操作的基本介绍,希望对你有做帮助!

  • 相关阅读:
    微信小程序开发——自定义堆叠图
    机器学习库Scikit-Learn
    Java面试题--JVM大厂篇之针对频繁的Minor GC问题,有哪些优化对象创建与使用的技巧可以分享?
    SVA断言总结
    Mac电脑版交互式原型设计软件 Axure RP 8汉化 for mac
    C++ Color the ball
    shell条件测试与条件测试操作符
    java计算机毕业设计呼和浩特市盈锐机电设备有限公司财务管理系统MyBatis+系统+LW文档+源码+调试部署
    CSV文件打开乱码解决方案
    联邦学习应用研究现状及发展趋势
  • 原文地址:https://blog.csdn.net/qq_43030934/article/details/133164140