Java后端开发注意事项

  |  

Java后端开发注意事项

异常处理

try-catch-finally

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
try
{
//代码可能存在异常
}
catch(Exception exception)
{
//捕获异常
//1.异常发生时,系统将异常封装成Exception对象exception
//2.传值给catch
//3.获取异常对象时,程序员自己处理
//4.若try中代码无异常,则catch不会发生
}
finally
{
//1.无论try中代码是否存在异常,始终执行finally
//2.通常将释放资源的代码,放在finally关闭
}

throws

main方法由JVM调用,main方法调用 f1 方法,f1 方法调用 f2 方法,如果 f2 方法发生异常,有两种方案,分别是 try-catch-finally 和 throws ,f2 使用 throws 方法,返回给 f1,f1也可以同样选择throws方法,返回给 main 方法,最终返回到 JVM。而 JVM 对处理异常的发生极为粗暴(1.输出异常信息。2. 退出程序)。

  • [x]

数组

数组复制

java.lang.System.arraycopy() 方法从指定的源数组复制一个数组,从指定位置开始,到目标数组的指定位置。 数组组件的子序列从 src 引用的源数组复制到 dest 引用的目标数组。复制的组件数等于 length< /b> 参数。

1
2
3
4
5
6
7
8
9
public static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length)
//将src数组里从索引为srcPos的元素开始, 复制到数组dest里的索引为destPos的位置, 复制的元素个数为length个.
//example
int[] arr1 ={1,2,3,4,5};arrayCopy(arr1, 3, arr1, 2, 2);
arr1 = {1,2,4,5,4,5} //相当于删除3

字符类型转换

ArrayList

ArrayList转int数组(流处理):
1
List.stream().mapToInt(Integer::intValue).toArray();
ArrayList转Integer数组(直接调用库函数):
1
2
Integer[] arr = new Integer[list.size()];
list.toArray(arr);//arr为相应的Integer数组
ArrayList转HashSet(流处理)
1
(HashMap<Integer>)List.stream().collect(Collectors.toSet());

HashSet

HashSet转int数组(流处理)
1
set.stream().mapToInt(x -> x).toArray();
HashSet转Integer数组
1
2
3
4
5
Object[] ins = set.toArray();
Integer[] i = new Integer[ins.length];
for(int k=0;k<ins.length;k++){
i[k] = Integer.parseInt(ins[k].toString());
}
HashSet转ArrayList:
1
(ArrayList<Integer>)set.stream().collect(Collectors.toList());

int[]

Int数组转ArryList
1
(ArrayList<Integer>) Arrays.stream(arr).boxed().collect(Collectors.toList());
Int数组转Integer数组
1
Arrays.stream(arr).boxed().toArray(Integer[]::new);
Int数组转HashSet
1
(HashSet<Integer>) Arrays.stream(arr).boxed().collect(Collectors.toSet());
Int数组转ArrayList
1
Arrays.copyOfRange(int[] temp, 0, length);

Integer[]

Integer数组转ArryList
1
(ArrayList<Integer>) Arrays.stream(integers).collect(Collectors.toList());
Integer数组转int数组
1
Arrays.stream(integers).mapToInt(Integer::valueOf).toArray(); 
Integer数组转HashSet
1
(ArrayList<Integer>) Arrays.stream(integers).collect(Collectors.toSet());

String

String转char[]字符数组
1
str.toCharArray();

String[]

String[]转ArrayList
1
String []result = (String[])arr.toArray(new String[arr.size()]);
ArrayList转String[]
1
String[] array = (String[])list.toArray(new String[size]);  

char

char转String
1
2
String.valueOf(c);
c+""
char转int
1
2
3
Integer.parseInt(String.valueOf(c));
Integer.parseInt(c+"");

HashMap

value值自加一

1
2
3
4
5
6
7
map.compute(i, (k, v) -> {
if (v == null) {
return 1;
}
return ++v;
});

replace和replaceAll

区别

1
2
3
4
5
6
7
replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSequence即字符串序列的意思,说白了也就是字符串);
replaceAll的参数是regex,即基于规则表达式的替换,比如:可以通过replaceAll("\\d", "*")把一个字符串所有的数字字符都换成星号;

相同点:都是全部替换,即把源字符串中的某一字符或字符串全部换成指定的字符或字符串;
不同点:replaceAll支持正则表达式,因此会对参数进行解析(两个参数均是),
如replaceAll("\\d", "*"),而replace则不会,replace("\\d","*")就是替换"\\d"的字符串,而不会解析为正则。

数组转换的方法(int[] -> int[])

方法1:for循环

1
2
3
4
int[] a = {1,2,3};
int[] b = new int[3];
for(int i = 0; i<a.length; i++)
b[i] = a[i];

方法2:Object的clone()

1
2
int[] a={1,2,3};
int[] b=(int[]) a.clone();

方法3:使用System的静态方法arraycopy()

1
2
3
int[] a ={1,2,3};
int[] b = new int[3];
System.arraycopy(a,0,b,0,3);

获取数组中的最大值和最小值

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public static void main(String[] args) {
int []nums = new int[]{1,5,6,8,2,4};
//方法1 排序后获取
Arrays.sort(nums);
System.out.println(nums[nums.length - 1]);//最大
System.out.println(nums[0]);//最小
//方法2 内置方法
Arrays.stream(nums).max().getAsInt();
Arrays.stream(nums).min().getAsInt();
}
}
}

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. Java后端开发注意事项
    1. 1.1. 异常处理
      1. 1.1.1. try-catch-finally
      2. 1.1.2. throws
    2. 1.2. 数组
      1. 1.2.1. 数组复制
    3. 1.3. 字符类型转换
      1. 1.3.1. ArrayList
        1. 1.3.1.1. ArrayList转int数组(流处理):
        2. 1.3.1.2. ArrayList转Integer数组(直接调用库函数):
        3. 1.3.1.3. ArrayList转HashSet(流处理)
      2. 1.3.2. HashSet
        1. 1.3.2.1. HashSet转int数组(流处理)
        2. 1.3.2.2. HashSet转Integer数组
        3. 1.3.2.3. HashSet转ArrayList:
      3. 1.3.3. int[]
        1. 1.3.3.1. Int数组转ArryList
        2. 1.3.3.2. Int数组转Integer数组
        3. 1.3.3.3. Int数组转HashSet
        4. 1.3.3.4. Int数组转ArrayList
      4. 1.3.4. Integer[]
        1. 1.3.4.1. Integer数组转ArryList
        2. 1.3.4.2. Integer数组转int数组
        3. 1.3.4.3. Integer数组转HashSet
      5. 1.3.5. String
        1. 1.3.5.1. String转char[]字符数组
      6. 1.3.6. String[]
        1. 1.3.6.1. String[]转ArrayList
        2. 1.3.6.2. ArrayList转String[]
      7. 1.3.7. char
        1. 1.3.7.1. char转String
        2. 1.3.7.2. char转int
    4. 1.4. HashMap
      1. 1.4.1. value值自加一
    5. 1.5. replace和replaceAll
      1. 1.5.1. 区别
    6. 1.6. 数组转换的方法(int[] -> int[])
      1. 1.6.1. 方法1:for循环
      2. 1.6.2. 方法2:Object的clone()
      3. 1.6.3. 方法3:使用System的静态方法arraycopy()
    7. 1.7. 获取数组中的最大值和最小值
载入天数...载入时分秒...
,
字数统计:36.3k