String array -> String ArrayList
package com.company;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class test12 {
public static void main(String[] args) {
String[] arr = {"A","B"}; // 배열
List<String> list = new ArrayList<>(Arrays.asList(arr)); // 배열 -> ArrayList
list.add(1,"C");//인덱스-값 추가
// 출력 1
for(int i=0; i<list.size(); i++){ //크기 구하기
System.out.println(list.get(i));
}
System.out.println("-------------------------------");
//출력 2
for(String i : list){
System.out.println(i);
}
System.out.println("-------------------------------");
list.remove(2);// 인덱스 삭제
//출력 3
Iterator iter = list.iterator();
while (iter.hasNext()){
System.out.println(iter.next());
}
System.out.println("-------------------------------");
//검색
System.out.println(list.contains("A")); //list에 1이 있는지 검색 : true
System.out.println(list.indexOf("A")); //1이 있는 index반환 없으면 -1
}
}
A
C
B
-------------------------------
A
C
B
-------------------------------
A
C
-------------------------------
true
0
String -> String 배열
String str = "hello world";
char[] a = str.toCharArray();//문자-> 문자 배열
for(int i=0; i<a.length; i++){
System.out.println(a[i]);// 출력
}
/*
h
e
l
l
o
w
o
r
l
d
*/
String -> int 배열
package com.company;
import java.util.Arrays;
public class test12 {
public static void main(String[] args) {
String str = "12345";
int[] array = new int[str.length()];
for(int i=0; i<str.length(); i++){
array[i] = str.charAt(i)-'0'; //문자 -> int 배열
}
// 출력 1
for(int j=0; j<array.length; j++){
System.out.println(array[j]);
}
/*
1
2
3
4
5
*/
// 출력 2
System.out.println(Arrays.toString(array));// [1, 2, 3, 4, 5]
}
}
String -> String 배열 -> int 배열
package com.company;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class test9 {
public static void main(String[] args) throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
//string -> string 배열
String[] a = rd.readLine().split(" ");
int[] b = new int[a.length];
int sum =0;
// string 배열 -> 형 변환 -> int 배열
for(int i=0; i<a.length; i++){
b[i] = Integer.parseInt(a[i]);
sum += b[i];
}
System.out.println(sum);
}//main
}//class
int -> int 배열
package com.company;
import java.util.Arrays;
public class test12 {
public static void main(String[] args) {
// int -> String -> int 배열
int num = 12345;
// 1) int -> String
String str = Integer.toString(num);
// 2) String -> int 배열
int[] digit = new int[str.length()];
for(int i=0; i<str.length();i++){
digit[i] = str.charAt(i)-'0'; //문자 -> 배열
}
System.out.println(Arrays.toString(digit));//출력 [1, 2, 3, 4, 5]
}
}
int[] -> String[]
값 하나만 바꿀때 String.valueOf(numbers[i]);
int[] number_array = {6, 10, 2};
String[] string_array = new String[number_array.length];
for(int i=0; i<number_array.length; i++){
string_array[i] = String.valueOf(number_array[i]);
}
int[] -> String
Arrays.toString(array);
'알고리즘 > 알고리즘 개념 정리' 카테고리의 다른 글
[java] Math 클래스, String (0) | 2021.09.27 |
---|---|
[java] stream (0) | 2021.09.27 |
[java] Comparable, Comparator (0) | 2021.09.27 |
[java] Array, List, Set, Map (0) | 2021.09.26 |
[java] 입력 Scanner BufferedReader 비교 (0) | 2021.09.25 |