String,StringBuilder,StringBuffer,数组,列表之间的若干转换以及若干数组常用操作方法

0. 几点说明

  • 本博客主要研究不同类型数据之间相互转换以及常见的操作,StringBuilder和StringBuffer主要差别在于前者非线程安全,后者线程安全,方法差别不大,因此下文只研究了StringBuilder的相关操作。
  • 为了更加深入的理解实现过程中相关变量的类型,建议使用getClass方法查看
  • ArrayList类型打印结果是以 “[ ]” 包围
  • 数组直接打印的不会出现数组中的元素,只会出现数组变量在堆中的地址。可以使用Arrays.toString()查看数组。
  • 数组,ArrayList,Set都可以使用foreach依次获取数据。

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: [hello, today, word, world]

 3.6 数组复制

String[] iArray = {"a", "d", "c", "k", "y"};
String[] jArray = Arrays.copyOf(iArray, 10);
String[] kArray = Arrays.copyOfRange(iArray, 2, 4);
System.out.println("copy1jArray: " + Arrays.toString(jArray));//  copy1jArray: [a, d, c, k, y, null, null, null, null, null]
System.out.println("copy2jArray: " + Arrays.toString(kArray));//  copy2jArray: [c, k]

3.7 数组比较

System.out.println(Arrays.equals(iArray, jArray));//  false
System.out.println(Arrays.equals(iArray, iArray));//  true

3.8 数组去重

String[] lArray = {"a", "d", "y", "c", "a", "k", "y"};
Set<String> set1 = new HashSet<String>(Arrays.asList(lArray));
System.out.println("set1: " + set1);//  set1: [a, c, d, y, k]
String[] mArray = new String[set1.size()];
set1.toArray(mArray);
System.out.println("mArray: " + Arrays.toString(mArray));//  mArray: [a, c, d, y, k]

3.9查找最小值

Arrays.sort(lArray);
System.out.println("minword: " + lArray[0]);//  minword: a

3.10 删除String中的一个字符

StringBuilder sb8 = new StringBuilder(str4);
sb8.deleteCharAt(3);
System.out.println("sb8: " + sb8);//  sb8: helo world

3.11 删除数组中的一个元素

ArrayList<String> arrList4 = new ArrayList<String>(Arrays.asList(mArray));
arrList4.remove(2);
System.out.println("arrList4: " + Arrays.toString(arrList4.toArray()));//  arrList4: [a, c, y, k]

参考链接:

http://c.biancheng.net/view/852.html

https://blog.csdn.net/renfufei/article/details/16829457

https://www.cnblogs.com/epeter/p/5664926.html

猜你喜欢

转载自blog.csdn.net/weixin_48968045/article/details/112298251