整理一下刷题过程中遇到的类
目录
类
String
String 创建的字符串存储在公共池中,而 new 创建的字符串对象在堆上:
.isEmpty()
boolean isEmpty()
如果此集合不包含元素,则返回true。
public class IsEmptyExample{
public static void main(String args[]){
String s1="";
String s2="javatpoint";
System.out.println(s1.isEmpty());
// expected output: true;
System.out.println(s2.isEmpty());
// expected output: false;
}
}
.toCharArray()
对字符串进行操作非常好用
public char[] toCharArray()
将此字符串转换成新的字符数组
class Solution {
public char firstUniqChar(String s) {
HashMap<Character, Boolean> dic = new HashMap<>();
char[] sc = s.toCharArray(); // 将输入的字符串s转换成数组sc
for(char c : sc){
dic.put(c, !dic.containsKey(c));
}
for(char c : sc){
if(dic.get(c)) return c;
}
return ' ';
}
}
.trim()
public String trim()
返回一个字符串,并值为此字符串,并删除任何前导和尾随空格
const greeting = ' Hello world! ';
console.log(greeting);
// expected output: " Hello world! ";
console.log(greeting.trim());
// expected output: "Hello world!";
.substring()
public String substring(int beginIndex, int endIndex)
返回一个字符串, 该字符串是此字符串的子字符串, 淄川开始于指定beginIndex
并延伸到字符索引endIndex - 1
。
const str = 'Mozilla';
console.log(str.substring(1, 3));
// expected output: "oz"
console.log(str.substring(2));
// expected output: "zilla"
ArrayList
ArrayList 是一个数组队列,提供了相关的添加、删除、修改、遍历等功能。
什么情况下适合使用ArrayList?
- 频繁访问列表中的某一个元素
- 只需要在列表的末尾增删元素
import java.util.ArrayList; // 引入 ArrayList 类
ArrayList<E> objectName =new ArrayList<>(); // 初始化
E: 泛型数据类型,用于设置 objectName 的数据类型,只能为引用数据类型。
objectName: 对象名。
.add()
添加元素到 ArrayList 可以使用 add() 方法
import java.util.ArrayList;
public class RunoobTest {
public static void main(String[] args) {
ArrayList<String> sites = new ArrayList<String>();
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
sites.add("Weibo");
System.out.println(sites);
}
}
.get()
访问 ArrayList 中的元素可以使用 get() 方法
输入:元素下标
import java.util.ArrayList;
public class RunoobTest {
public static void main(String[] args) {
ArrayList<String> sites = new ArrayList<String>();
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
sites.add("Weibo");
System.out.println(sites.get(1)); // 访问第二个元素
}
}
.set()
修改 ArrayList 中的元素可以使用 set() 方法
输入:(下标索引位置, 要修改成的值)
LinkedList
LinkedList类实现了queue接口!
什么情况下适合使用LinkedList?
- 需要通过循环迭代访问列表中的某一个元素
- 需要频繁地在在列表开头, 中间, 末尾增删元素
LinkedList 继承了 AbstractSequentialList 类。
LinkedList 实现了 Queue 接口,可作为队列使用。
LinkedList 实现了 List 接口,可进行列表的相关操作。
LinkedList 实现了 Deque 接口,可作为队列使用。
LinkedList 实现了 Cloneable 接口,可实现克隆。
LinkedList 实现了 java.io.Serializable 接口,即可支持序列化,能通过序列化去传输。
想要在程序中实现队列, 需要:
import java.util.LinkedList;
import java.util.Queue;
对象的创建:
Queue<String> queue = new LinkedList<String>();
Deque
HashSet
HashSet 基于 HashMap 来实现的,是一个不允许有重复元素的集合。
实现的是Set接口
HashSet 允许有 null 值。
HashSet 是无序的,即不会记录插入的顺序。
HashSet 不是线程安全的, 如果多个线程尝试同时修改 HashSet,则最终结果是不确定的。 您必须在多线程访问时显式同步对 HashSet 的并发访问。
在程序中实现HashSet需要:
import java.util.HashSet; // 引入 HashSet 类
对象的创建:
HashSet<String> sites = new HashSet<String>();
.add()
添加元素, 重复的元素不会被重复添加
// 引入 HashSet 类
import java.util.HashSet;
public class RunoobTest {
public static void main(String[] args) {
HashSet<String> sites = new HashSet<String>();
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
sites.add("Zhihu");
sites.add("Runoob"); // 重复的元素不会被添加
System.out.println(sites); //[Google, Runoob, Zhihu, Taobao]
}
}
.contains()
判断元素是否存在在集合中,若存在,返回true
// 引入 HashSet 类
import java.util.HashSet;
public class RunoobTest {
public static void main(String[] args) {
HashSet<String> sites = new HashSet<String>();
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
sites.add("Zhihu");
sites.add("Runoob"); // 重复的元素不会被添加
System.out.println(sites.contains("Taobao")); // true
}
}
.remove()
删除元素
.clear()
删除所有元素
.size()
计算大小
HashMap
HashMap继承自AbstractMap类, 实现Map接口
HashMap 是一个散列表,它存储的内容是键值对(key-value)映射。
HashMap 实现了 Map 接口,根据键的 HashCode 值存储数据,具有很快的访问速度,最多允许一条记录的键为 null,不支持线程同步。
HashMap 是无序的,即不会记录插入的顺序。
HashMap 继承于AbstractMap,实现了 Map、Cloneable、java.io.Serializable 接口。
key和value的值可以相同, 也可以不同.
使用之前需要引用它
import java.util.HashMap; // 引入 HashMap 类
创建对象方法:
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
如数组中重复的数字:
class Solution{
public int findRepeatNumber(int[] nums){
HashSet<Integer> set = new HashSet<Integer>();
for(int x : nums){
// 如果找到相同的数
if(set.contains(x)){
return x;
}//判断结束
// 如果没有找到相同的数
set.add(x);
}//循环结束
return -1;
}
}
.put()
添加键值对(key-value)可以使用 put() 方法:
// 引入 HashMap 类
import java.util.HashMap;
public class RunoobTest {
public static void main(String[] args) {
// 创建 HashMap 对象 Sites
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
// 添加键值对
Sites.put(1, "Google");
Sites.put(2, "Runoob");
Sites.put(3, "Taobao");
Sites.put(4, "Zhihu");
System.out.println(Sites); //{1=Google, 2=Runoob, 3=Taobao, 4=Zhihu}
}
}
创建一个字符串(String)类型的 key 和字符串(String)类型的 value
// 引入 HashMap 类
import java.util.HashMap;
public class RunoobTest {
public static void main(String[] args) {
// 创建 HashMap 对象 Sites
HashMap<String, String> Sites = new HashMap<String, String>();
// 添加键值对
Sites.put("one", "Google");
Sites.put("two", "Runoob");
Sites.put("three", "Taobao");
Sites.put("four", "Zhihu");
System.out.println(Sites);
}
}
.get()
使用 get(key) 方法来获取 key 对应的 value
// 引入 HashMap 类
import java.util.HashMap;
public class RunoobTest {
public static void main(String[] args) {
// 创建 HashMap 对象 Sites
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
// 添加键值对
Sites.put(1, "Google");
Sites.put(2, "Runoob");
Sites.put(3, "Taobao");
Sites.put(4, "Zhihu");
System.out.println(Sites.get(3)); // Taobao
}
}
.remove()
使用 remove(key) 方法来删除 key 对应的键值对(key-value)
// 引入 HashMap 类
import java.util.HashMap;
public class RunoobTest {
public static void main(String[] args) {
// 创建 HashMap 对象 Sites
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
// 添加键值对
Sites.put(1, "Google");
Sites.put(2, "Runoob");
Sites.put(3, "Taobao");
Sites.put(4, "Zhihu");
Sites.remove(4);
System.out.println(Sites); //{1=Google, 2=Runoob, 3=Taobao}
}
}
.clear()
删除所有键值对(key-value)可以使用 clear 方法:
// 引入 HashMap 类
import java.util.HashMap;
public class RunoobTest {
public static void main(String[] args) {
// 创建 HashMap 对象 Sites
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
// 添加键值对
Sites.put(1, "Google");
Sites.put(2, "Runoob");
Sites.put(3, "Taobao");
Sites.put(4, "Zhihu");
Sites.clear();
System.out.println(Sites);
}
}
.size()
计算大小
BufferedReader