蚂蚁3期JAVA互联网架构师项目实战(完整)

1. String,StringBuilder,数组,列表声明
1.1 声明数组

//注意数组必须要声明大小,如果无法确定,需要使用列表
String[] aArray = new String[5];
String[] bArray = {"a", "b", "c", "d", "e"};
String[] cArray = new String[]{"a", "b", "c", "d", "e"};
1.2 声明一个String变量

String str = "hello world";
1.3 声明一个StringBuilder(StringBuffer)变量

StringBuilder sb1 = new StringBuilder();//  构造一个空的字符串缓冲区,并且初始化为 16 个字符的容量。
StringBuilder sb2 = new StringBuilder(10);//  创建一个空的字符串缓冲区,并且初始化为指定长度 length 的容量。
StringBuilder sb3 = new StringBuilder("hello world");//  创建一个字符串缓冲区,并将其内容初始化为指定的字符串内容str,字符串缓冲区的初始容量为 16 加上字符串str的长度。
 
System.out.println("sb3: " + sb3.append(" java"));//  sb3: hello world java
1.4 声明一个ArrayList变量

ArrayList<String> arrList = new ArrayList<String>();
arrList.add("hello ");
arrList.add("world");
System.out.println("arrList: " + arrList);//  arrList: [hello , world]
2. char,String,StringBuilder,数组,列表之间相互转换
2.1 数组 to ArrayList

ArrayList<String> arrList1 = new ArrayList<String>(Arrays.asList(cArray));
System.out.println("arrList1: " + arrList1);//  arrList1: [a, b, c, d, e]
2.2 ArrayList to 数组

String[] dArray = new String[arrList1.size()];
arrList1.toArray(dArray);
System.out.println("dArray: " + Arrays.toString(dArray));//  dArray: [a, b, c, d, e]
2.3 String to StringBuilder

String str1 = "hello ";
String str2 = "world";
StringBuilder sb4 = new StringBuilder();
sb4.append(str1).append(str2);
System.out.println("sb4: " + sb4);//  sb4: hello world
2.4 StringBuilder to String

System.out.println("sb4:" + sb4.toString());//  sb4:hello world
System.out.println("sb4.class: " + sb4.toString().getClass());//  sb4.class: class java.lang.String
2.5 String to ArrayList

ArrayList<String> arrList2 = new ArrayList<String>();
//arrList2.add(str).add(str1); 错误
arrList2.add(str);
arrList2.add(str1);
arrList2.add(str2);
System.out.println("arrList2: " + arrList2);//  arrList2: [hello world, hello , world]
2.6 ArrayList to String

String[] eArray = new String[arrList2.size()];
System.out.println("eArray: " + Arrays.toString(arrList2.toArray(eArray)));//  eArray: [hello world, hello , world]
2.7 String to String[]

String[] fArray = new String[] {str, str1, str2};
System.out.println("fArray: " + Arrays.toString(fArray));//  fArray: [hello world, hello , world]
2.8 String[] to String

StringBuilder sb5 = new StringBuilder();
for(String tmp:fArray) {
    sb5.append(tmp);
}
System.out.println("sb5: " + sb5.toString());//  sb5: hello worldhello world
2.9 数组 to StringBuilder

//2.9 数组 to StringBuilder
StringBuilder sb6 = new StringBuilder();
for(int i=0;i<fArray.length;i++) {
    sb6.append(fArray[i]);
}
System.out.println("sb6: " + sb6);//  sb6: hello worldhello world
2.10 StringBuilder to 数组

String[] gArray = new String[] {sb4.toString(), sb6.toString()};
System.out.println("gArray: " + Arrays.toString(gArray));//  gArray: [hello world, hello worldhello world]
2.11 ArrayList to StringBuilder

StringBuilder sb7 = new StringBuilder();
for(String tmp:arrList2) {
    sb7.append(tmp);
}
System.out.println("sb7: " + sb7);//  sb7: hello worldhello world
2.12 StringBuilder to ArrayList

ArrayList<String> arrList3 = new ArrayList<String>();
arrList3.add(sb6.toString());
arrList3.add(sb7.toString());
System.out.println("arrList3: " + arrList3);//  arrList3: [hello worldhello world, hello worldhello world]
2.13 String to char[] 

String str3 = "hello world";
char[] ch = str3.toCharArray();
System.out.println("ch: " + Arrays.toString(ch));//  ch: [h, e, l, l, o,  , w, o, r, l, d]
2.14 char[] to String  

String str4 = String.valueOf(ch);
System.out.println("str4: " + str4);//  str4: hello world
 3. 数组常用操作方法
3.1 检查数组中是否含有某个变量

System.out.println(Arrays.asList(gArray).contains("hello"));//  false
System.out.println(Arrays.asList(gArray).contains("hello world"));//  true
3.2 数组中的元素翻转

Collections.reverse(Arrays.asList(gArray));
System.out.println("gArray: " + Arrays.toString(gArray));//  gArray: [hello worldhello world, hello world]
3.3 数组转Set

Set<String> set = new HashSet<String>(Arrays.asList(gArray));
System.out.println("set: " + set);//  set: [hello worldhello world, hello world]
3.4 int数组转成string数组

int[] num = {1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println("num: " + Arrays.toString(num));//  num: [1, 2, 3, 4, 5, 6, 7, 8, 9]
3.5 数组排序

int[] num1 = {2, 4, 3, 1, 6, 7, 5, 8, 9, 0};
String[] hArray = {"world", "hello", "today", "word"};
Arrays.sort(num1, 4, 9);
System.out.println("originnum1: " + Arrays.toString(num1));//  originnum1: [2, 4, 3, 1, 5, 6, 7, 8, 9, 0]
System.out.println("sort1num1: " + Arrays.toString(num1));//  sort1num1: [2, 4, 3, 1, 5, 6, 7, 8, 9, 0]
Arrays.sort(num1);
System.out.println("sort2num1: " + Arrays.toString(num1));//  sort2num1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Arrays.sort(hArray);
System.out.println("sort3hArray: " + Arrays.toString(hArray));//  sort3hArray:
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值