当前位置: 首页 > news >正文

集合和字符串的使用

文章目录

  • 一、集合概述
  • 二、Collection
    • 2.1 List接口
    • 2.2 Set接口(不常用)
      • 2.2.1 TreeSet
  • 三、Map接口
  • 四、Collections工具类
  • 五、String
  • 六、String类型转换
    • 6.1 基本数据类型
    • 6.2 基本数据类型、包装类 --> String
    • 6.3 String与char[]
    • 6.4 String与byte[]


一、集合概述

  • Collection接口:单列数据
    • List接口:元素有序、可重复的集合 “动态数组”
      • ArrayList、LinkedList、Vector
    • Set接口:元素无序、不可重复的集合 “集合”
      • HashSet(子类:LinkedHashSet)、TreeSet
  • Map接口:双列数据
    • HashMap、LinkedHashMap、TreeMap、Hashtable、Properties

二、Collection

需重写equals()

  • add(Objec tobj)
  • addAll(Collection coll)
  • int size()
  • void clear()
  • boolean isEmpty()
  • boolean contains(Object obj)
  • boolean containsAll(Collection c)
  • boolean remove(Object obj)
  • boolean removeAll(Collection coll)
  • boolean retainAll(Collection c):把交集的结果存在当前集合中,不影响c
  • boolean equals(Object obj)
  • Object[] toArray() :转成对象数组
  • hashCode():获取集合对象的哈希值
  • iterator() : :hasNext()、next()、remove()

2.1 List接口

  • void add(intindex, Object ele)
  • boolean addAll(int index, Collection eles)
  • Object get(int index)
  • int indexOf(Object obj):返回obj在集合中首次出现的位置
  • int lastIndexOf(Object obj): 返回obj在当前集合中末次出现的位置
  • Object remove(int index):移除指定index位置的元素,并返回此元素
  • Object set(int index, Object ele)
  • List subList(int fromIndex, int toIndex)

2.2 Set接口(不常用)

Set需重写hashCode()和equals()
HashSet(子类:LinkedHashSet)无额外方法,

2.2.1 TreeSet

Comparator comparator() 两种排序方式:自然排序(实现Comparable接口) 和 定制排序(Comparator)

三、Map接口

Map中的key用Set来存放,不允许重复,需重写hashCode()和equals()方法。

  • Object put(Object key,Object value)
  • void putAll(Map m)
  • Object remove(Object key
  • void clear()
  • Object get(Object key)
  • boolean containsKey(Object key)
  • boolean containsValue(Object value)
  • int size()
  • boolean isEmpty()
  • boolean equals(Object obj)
  • Set keySet()
  • Collection values()
  • Set entrySet()

四、Collections工具类

  • reverse(List)
  • shuffle(List)
  • sort(List)
  • sort(List,Comparator)
  • swap(List,int, int)
  • Object max(Collection)
  • Object max(Collection,Comparator)
  • Object min(Collection)
  • Object min(Collection,Comparator)
  • int frequency(Collection,Object)
  • void copy(List dest,List src)
  • boolean replaceAll(List list,Object oldVal,Object newVal)

五、String

  • int length()
  • char charAt(int index)
  • boolean isEmpty()
  • String toLowerCase()
  • String toUpperCase()
  • String trim()
  • boolean equals(Object obj)
  • boolean equals IgnoreCase(String anotherString)
  • String concat(String str)
  • int compareTo(String anotherString)
  • String substring(int beginIndex)
  • String substring(int beginIndex,int endIndex)
  • boolean endsWith(String suffix)
  • boolean startsWith(String prefix)
  • boolean startsWith(String prefix, int toffset)
  • boolean contains(CharSequence s)
  • int indexOf(String str)
  • int indexOf(String str, int fromIndex)
  • int lastIndexOf(String str)
  • int lastIndexOf(String str, int fromIndex)
  • String replace(char oldChar, char newChar)
  • String replace(CharSequence target, CharSequence replacement)
  • String replaceAll(String regex, String replacement)
  • String replaceFirst(String regex, String replacement)
  • boolean matches(String regex):告知此字符串是否匹配给定的正则表达式
  • String[] split(String regex)
  • String[] split(String regex, int limit)

六、String类型转换

6.1 基本数据类型

String --> 基本数据类型、包装类:调用包装类的静态方法:parseXxx(str)
int num = Integer.parseInt(str1);

6.2 基本数据类型、包装类 --> String

调用String重载的valueOf(xxx)
String str2 = String.valueOf(num);

6.3 String与char[]

String --> char[]:
调用String的toCharArray() char[] charArray = str1.toCharArray();

char[] --> String:
调用String的构造器 String str2 = new String(arr);

6.4 String与byte[]

http://www.lryc.cn/news/327015.html

相关文章:

  • Wagtail-基于Python Django的内容管理系统CMS实现公网访问
  • Python入门级题目及答案
  • 【C语言基础】:字符串函数(二)
  • 【Docker】Docker资源(创建容器)CPU/内存/磁盘IO/GPU限制与分配教程
  • 发展规划--IM系统
  • stm32平衡车
  • google浏览器下载文件提示无法安全地下载怎么解决?
  • Navicat 干货 | 通过检查约束确保 PostgreSQL 的数据完整性
  • FPGA时钟资源详解(2)——Clock-Capable Inputs
  • 使用JMeter的JSON提取器:通过递归下降查找,从接口响应中提取特定字段
  • Js全部循环方法解析
  • 高阶SQL语句(二)
  • Phoenix伪分布安装
  • Python算法100例-4.6 歌星大奖赛
  • 静态路由表学习实验
  • 客户端测试 可测性改进-学习记录
  • 机器学习和神经网络9
  • http模块—http请求练习
  • 视频号原视频下载使用方法?新人都在用
  • 用html画一个烟花特效
  • SQL-CRUD-1
  • linux 命令行下的计算器
  • Available platform plugins are: linuxfb, minimal, offscreen, vnc.
  • C++中string容器的修改操作
  • Elasticsearch:虚拟形象辅助和对话驱动的语音到 RAG 搜索
  • 测试开发工程师(QA)职业到底需要干些什么?part7:硬件测试工程师QA
  • Python基础:标准库 -- pprint (数据美化输出)
  • Visual Studio 小更新:改善变量的可见性
  • C++自主点餐系统
  • jconsole jvisualvm