leetcode常用的一些转换
Arrays数组常用的操作
import java.util.Arrays;
public class ArrayTest {
public static void main(String[] args) {
//1.声明数组
int[] arr1; //推荐
int arr2[];
//2.创建数组
int[] arr3 = new int[2];
int[] arr4 = {1,3,4,5};
//3.数组赋值 arr[i] = n;
//4.遍历数组,下标从0开始;长度:数组名.length
for (int i = 0; i < arr3.length; i++) {
arr3[i] = i;
System.out.println(arr3[i]);
}
//4. for-each遍历
for (int num : arr3) {
System.out.println(num);
}
//5. 二维数组,如定义一个3*4的二维数组
int[][] arr5 = new int[3][4];
System.out.println("arr5的行:" + arr5.length);
System.out.println("arr5的列:" + arr5[0].length);
//6. Arrays类 java.util.Arrays
//6.1 sort方法
int nums[] = {2,5,-1,23,111};
System.out.println("排序前:");
for (int num : nums) {
System.out.println(num + ",");
}
Arrays.sort(nums);
System.out.println("排序后:");
for (int num : nums) {
System.out.println(num + ",");
}
//6.2 toString方法, 返回数组的字符串形式;
System.out.println(Arrays.toString(nums));
}
}
String的常用操作
import java.util.Arrays;
public class StringTest {
public static void main(String[] args) {
//1.创建字符串, 值不能变,String 创建的字符串存储在公共池中,而 new 创建的字符串对象在堆上
String str = "zhangsan";
String str2 = new String("zhangsan");
System.out.println(str == str2);// false
//2. 字符串长度
System.out.println(str.length());
//3. 字符串连接 +/concat
System.out.println(str+str2);
System.out.println(str.concat(str2));
//4. charAt() 返回指定索引处的字符, 0 到 length() - 1
System.out.println(str.charAt(3)); // n
//5. 判空isEmpty()
System.out.println(str.isEmpty());
// String转字符数组
String resStr = "1234545";
char arrRes[] = resStr.toCharArray(); // 转Char型数组
System.out.println(arrRes);
//Arrays的toString方法来打印数组中的数据
String strRes = Arrays.toString(arrRes);
System.out.println(strRes);
// 字符数组转String:
System.out.println("字符数组转String:");
// 1. String的构造函数
String strRes2 = new String(arrRes);
System.out.println(strRes2);
// 2. String类的ValueOf方法
String strRes3 = String.valueOf(arrRes);
System.out.println(strRes3);
}
}
StringBuilder的常用操作
public class StringBuilderTest {
public static void main(String[] args) {
// 1.创建字符串
StringBuilder sb = new StringBuilder(10);
// 增
sb.append("www.");
sb.append("cainiao");
sb.append(".com");
sb.append(20);
System.out.println(sb); //www.cainiao.com20
// 删
sb.delete(2,3);
System.out.println(sb); //ww.cainiao.com20
// 改
StringBuffer sb2 = new StringBuffer("www.baidu.com");
sb2.replace(0,2, "123");
System.out.println(sb2); //123w.baidu.com
// setCharAt(index, ch);
sb2.setCharAt(0,'我'); //我23w.baidu.com
System.out.println(sb2);
// 查
// 某个位置的字符
System.out.println(sb2.charAt(3));//w
// 子字符串出现的索引
System.out.println(sb2.indexOf("baidu")); //5
// 插
StringBuffer sb3 = new StringBuffer("www.baidu.com");
sb3.insert(0, "百度");
System.out.println(sb3); //百度www.baidu.com
// 长度
System.out.println(sb3.length());//15
// StringBuilder转String
String strRes = sb3.toString();
System.out.println(strRes); //百度www.baidu.com
// String转StringBuilder: 利用构造函数
StringBuilder sbRes = new StringBuilder(strRes);
System.out.println(sbRes);
// StringBuilder转字符数组:1.先转成String,2.再toCharArray()转成字符数组
StringBuilder sbRes2 = new StringBuilder("www.bbb.com");
String sbRes2Str = sbRes2.toString();
char[] resSbRes2Str = sbRes2Str.toCharArray();
System.out.println(resSbRes2Str);
// 字符数组转StringBuilder: 先转String,在转StringBuilder
String resSB = new String(resSbRes2Str);
System.out.println(resSB);
StringBuilder sbRes3 = new StringBuilder(resSB);
System.out.println(sbRes3);
}
}
ArraList的常用操作
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class ArrayListTest {
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
// 1. 增
arrayList.add("baidu");
arrayList.add("google");
arrayList.add("ali");
System.out.println(arrayList);
// 2. 删
arrayList.remove(2);
System.out.println(arrayList);
arrayList.add("hw");
// 3. 改
System.out.println("修改前:" + arrayList);
arrayList.set(2, "taobao");
System.out.println("修改后:" + arrayList);
// 4. 查
System.out.println(arrayList.get(1));
// 5. 长度
System.out.println(arrayList.size());
// 遍历
for (int i = 0; i < arrayList.size(); i++) {
System.out.println(arrayList.get(i));
}
// for-each
for (String str : arrayList) {
System.out.println(str);
}
// 排序
System.out.println("排序");
Collections.sort(arrayList);
System.out.println(arrayList);
// 判断空
System.out.println(arrayList.isEmpty());
// 判断是否存在
System.out.println(arrayList.contains("baidu"));
// ArrayList转普通数组
String[] arr = new String[arrayList.size()];
arrayList.toArray(arr); // 转成String数组
System.out.println(Arrays.toString(arr));
// ArrayList转字符串, 用 toString() 方法将 arraylist 转换为字符串,该方法将整个 arraylist 转换为一个 String 类型
String resList = arrayList.toString();
System.out.println(resList);
// 普通数组转ArrayList
// ArrayList<T> arraylist = new ArrayList<T>(Arrays.asList(arrayname));
// 使用Arrays.asList方法将Array转换为ArrayList。
Integer[] intArr = {1,2,3,45,6};
ArrayList<Integer> list2 = new ArrayList<>(Arrays.asList(intArr));
System.out.println(list2);
// 手动方式
ArrayList<Integer> list3 = new ArrayList<>();
for (Integer num : intArr) {
list3.add(num);
}
System.out.println(list3);
}
}
HashMap的常用操作
import java.util.HashMap;
public class HashMapTest {
public static void main(String[] args) {
HashMap<Integer, String> hmap = new HashMap<>();
// 增
hmap.put(1, "baidu");
hmap.put(2, "google");
hmap.put(3, "java");
hmap.put(4, "python");
System.out.println(hmap); // {1=baidu, 2=google, 3=java, 4=python}
// 删
hmap.remove(4);
System.out.println(hmap); //{1=baidu, 2=google, 3=java}
// 改
hmap.put(3, "c++");
System.out.println(hmap); //{1=baidu, 2=google, 3=c++}
// replace
replaceTest();
// 查。 get(key)
// 遍历
for (Integer i : hmap.keySet()) {
System.out.println(i + ":" + hmap.get(i));
}
// 遍历迭代 HashMap 中的所有值。
for (String value : hmap.values()) {
System.out.println(value);
}
// containsKey(key)
System.out.println(hmap.containsKey(3)); //true
// getOrDefault(key, 0), 获取指定 key 对应对 value,如果找不到 key ,则返回设置的默认值
System.out.println(hmap.getOrDefault(2, "哈哈")); //google,能找到
System.out.println(hmap.getOrDefault(11, "哈哈")); // 哈哈,找不到
}
public static void replaceTest() {
HashMap<Integer, String> hmap = new HashMap<>();
// 增
hmap.put(1, "baidu");
hmap.put(2, "google");
hmap.put(3, "java");
hmap.put(4, "python");
// hashmap.replace(K key, V newValue) :
// hashmap.replace(K key, V oldValue, V newValue)
// key 存在,返回旧值
String oldValue = hmap.replace(2, "zhihu");
System.out.println("replace方法返回oldValue: " + oldValue); //replace方法返回oldValue: google
System.out.println(hmap); //{1=baidu, 2=zhihu, 3=java, 4=python}
// key 不存在,返回null
String value = hmap.replace(10, "gao");
System.out.println("key 10 返回的值:" + value); //null
System.out.println(hmap); //{1=baidu, 2=zhihu, 3=java, 4=python}
// key 存在, oldValue 存在,返回true,替换成功。否则返回false,替换不成功。
boolean res = hmap.replace(2, "www", "haha");
System.out.println(res); // false
System.out.println(hmap); // {1=baidu, 2=zhihu, 3=java, 4=python}
boolean res2 = hmap.replace(2, "zhihu", "haha");
System.out.println(res2); //true
System.out.println(hmap); // {1=baidu, 2=haha, 3=java, 4=python}
// key不存在, 返回false,
boolean res3 = hmap.replace(20, "zhihu", "haha");
System.out.println(res3); //false
System.out.println(hmap); // {1=baidu, 2=haha, 3=java, 4=python}
}
}
HashSet的常用操作
import java.util.HashSet;
public class HashSetTest {
public static void main(String[] args) {
HashSet<String> hset = new HashSet<>();
// 增
hset.add("baidu");
hset.add("google");
hset.add("zhihu");
hset.add("ali");
hset.add("baidu");
System.out.println(hset); //[zhihu, baidu, google, ali]
// 删
hset.remove("ali");
System.out.println(hset);
// 改
// contains()
// isEmpty()
// size()
// 遍历
for (String str : hset) {
System.out.println(str);
}
}
}
包装类和String,基本数据类型常见的基本转换
基本数据类型->String类
- String类的valueOf()方法:String fs = String.valueOf(2.34f);
- 更直接的方式
5+""
String类->基本数据类型
- 通过包装类的构造器实现:int i = new Integer("12")
- 通过调用包装类的parseXxx(字符串)静态方法:Float f = Float.parseFloat("12.1");
包装类->String类
- 包装类对象的toString()方法
Integer t = new Integer(100);
String s = t.toString();// s = "100";
- 调用包装类的toString(形参)方法
String s1 = Integer.toString(314); // s1= "314" 将数字转换成字符串。
String类->包装类
通过字符串参数Float f = new Float("3.14f");
本文作者:benjieqiang
本文链接:https://www.cnblogs.com/benjieqiang/p/17196413.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步