String、StringBuilder、StringBuffer、配列、リスト、およびいくつかの配列の一般的な操作メソッド間のいくつかの変換

0.いくつかのメモ

  • このブログでは、主にさまざまな種類のデータと一般的な操作の間の変換について説明しています。StringBuilderとStringBufferの主な違いは、前者はスレッドセーフではなく、後者はスレッドセーフであるということです。メソッドにほとんど違いはありません。したがって、 StringBuilderの関連する操作を以下で説明します。
  • 実装プロセスで関連する変数のタイプをより深く理解するために、getClassメソッドを使用して表示することをお勧めします。
  • ArrayListタイプの印刷結果は「[]」で囲まれています
  • 配列が直接出力される場合、配列内の要素は表示されず、ヒープ内の配列変数のアドレスのみが表示されます。Arrays.toString()を使用して配列を表示できます。
  • Array、ArrayList、Setは、foreachを使用してデータを順番に取得できます。

1.文字列、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 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.4ArrayList変数を宣言する

ArrayList<String> arrList = new ArrayList<String>();
arrList.add("hello ");
arrList.add("world");
System.out.println("arrList: " + arrList);//  arrList: [hello , world]

2. char、String、StringBuilder、array、listの間で変換します

2.1配列からArrayListへ

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

2.2ArrayListからArrayへ

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

2.3文字列から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.4StringBuilderから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文字列から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.6ArrayListからStringへ

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

2.7文字列から文字列[]

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

2.8 String []からString

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

2.9配列から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.10StringBuilderから配列へ

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

2.11ArrayListからStringBuilderへ

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

2.12StringBuilderから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文字列から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文字[]から文字列  

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<String> set = new HashSet<String>(Arrays.asList(gArray));
System.out.println("set: " + set);//  set: [hello worldhello world, hello world]

3.4int配列を文字列配列に変換する

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文字列内の文字を削除する

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