Java Basic

字符串操作

字符串比较

1
2
boolean result = str1.equals(str2);
boolean result = str1.equalsIgnoreCase(str2);

搜索与检索

1
2
3
4
5
6
7
int result = str1.indexOf(str2);
// 从字符串的第 index 个数开始查找
int result = str1.indexOf(str2, index);
// 从第 index1 数取到最后
String index = str1.substring(index1);
// 范围 [index1, index2)
String index = str1.substring(index1, index2);

字符串反转

1
2
StringBuilder sb = new StringBuilder(string);
String str = sb.reverse().toString();

按单词的字符串反转

1
2
3
4
5
6
7
8
9
10
11
String str = "hello world";
Deque<String> stack = new ArrayDeque();
StringTokenizer strTok = new StringTokenizer(str);
while(strTok.hasMoreTokens()){
stack.push(strTok.nextElement());
}
StringBuilder sb = new StringBuilder();
while(!stack.empty()) {
sb.append(stack.pop());
sb.append(" ");
}

大小写转化

1
2
String strUpper = str1.toUpperCase();
String strLower = str1.toLowerCase();

首尾空格移除

1
2
String str1 = " asdfsdf ";
str1.trim();

空格移除

1
str1.replace(" ","");

字符串转化为数组

1
2
String str = "tim,kerry,timmy,camden";
String[] results = str.split(",");

数据结构

重置数组大小

1
2
3
4
5
int[] myArray = new int[10];
int[] tmp = new int[myArray.length + 10];
System.arraycopy(myArray, 0, tmp, 0, myArray.length);
myArray = tmp;

Map 遍历

1
2
3
4
5
for (Iterator it = map.entrySet().iterator();it.hasNext();){
Map.Entry entry = (Map.Entry)it.next();
Object key = entry.getKey();
Object value = entry.getValue();
}

映射集合

1
2
3
4
5
6
7
HashMap<Type> map = new HashMap<>();
map.put(key, value);
// find key or value
map.containsKey(key);
map.containsValue(value);
// 将 hashmap 转化为 array
Object[] objects = hashmap.entrySet().toArray();

数组和列表

1
2
3
4
5
6
7
8
int[] nums = {1,4,7,324,0,-4};
Arrays.sort(nums);
// binary search
int index = Arrays.binarySearch(nums, number);
// 列表排序
Collections.sort(List)
// arrayList 转化为 array
Object[] objects = arrayList.toArray();

Set

  • TreeSet排序默认是升序,想要实现自定义排序可以通过传进去一个Comparator或者TreeSet的添加对象实现Comparator接口
  • 遍历

    • foreach 遍历
    • 迭代器遍历

      1
      2
      3
      4
      Iterator<Integer> iterator =hashSet.iterator();
      while (iterator.hasNext()){
      Integer integer=iterator.next();
      }
    • 需要重写 hashCode() 和 equals() 方法

基本数据结构

比较 Double

1
2
3
Double a = 4.5;
Double b = 4.5;
boolean result = a.equals(b);

二进制、八进制、十六进制转换

1
2
3
4
int val = 25;
String binaryStr = Integer.toBinaryString(val);
String octalStr = Integer.toOctalString(val);
String hexStr = Integer.toHexString(val);

随机数生成

1
2
3
4
double rn = Math.random();
// 生产正太分布的数
// mean, var
double value = Math.sqrt(var)*random.nextGaussian()+mean

格式化数字

1
2
3
4
double value = 2343.8798;
NumberFormat numberFormatter = NumberFormat.getNumberInstance();
String formattedValue = numberFormatter.format(value);
System.out.format("%s%n",formattedValue);
文章目录
  1. 1. 字符串操作
    1. 1.1. 字符串比较
    2. 1.2. 搜索与检索
    3. 1.3. 字符串反转
    4. 1.4. 按单词的字符串反转
    5. 1.5. 大小写转化
    6. 1.6. 首尾空格移除
    7. 1.7. 空格移除
    8. 1.8. 字符串转化为数组
  2. 2. 数据结构
    1. 2.1. 重置数组大小
    2. 2.2. Map 遍历
    3. 2.3. 映射集合
    4. 2.4. 数组和列表
    5. 2.5. Set
  3. 3. 基本数据结构
    1. 3.1. 比较 Double
    2. 3.2. 二进制、八进制、十六进制转换
    3. 3.3. 随机数生成
    4. 3.4. 格式化数字
|