ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Java 변환
    잡다한 것들 2022. 8. 20. 10:46
    반응형

    int[] to Integer[]

    import java.util.Arrays;
    
    public class Main {
        public static void main(String[] args) {
            int[] a = {1,2,3,4};
            Integer[] b = Arrays.stream(a)
                    .boxed()
                    .toArray(Integer[]::new);
            System.out.println(Arrays.toString(b));
        }
    }

    Integer[] to int []

    import java.util.Arrays;
    
    public class Main {
        public static void main(String[] args) {
            Integer[] a = {1,2,3,4};
            int[] b = Arrays.stream(a).mapToInt(Integer::intValue).toArray();
            System.out.println(Arrays.toString(b));
        }
    }

    int[] to string[]

    import java.util.*;
    
    public class Main {
        public static void main(String[] args) {
            int a = 123;
            String b = String.valueOf(a);
            System.out.println(b);  // 123
    
            int[] c = {1, 55, 129};
            String[] d = Arrays.stream(c).mapToObj(String::valueOf).toArray(String[]::new);
            System.out.println(Arrays.toString(d)); // [1, 55, 129]
        }
    }

    string[] to int[]

    import java.util.*;
    
    public class Main {
        public static void main(String[] args) {
            String[] a = {"1", "2", "3", "4"};
            int[] b = Arrays.stream(a).mapToInt(Integer::valueOf).toArray();
            System.out.println(Arrays.toString(b));
        }
    }

    int[] to List<Integer>

    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    
    public class Main {
        public static void main(String[] args) {
            int[] arr = {1, 2, 3, 4, 5};
            List<Integer> list = Arrays.stream(arr)  // IntStream
                    .boxed()  // Stream<Integer>
                    .collect(Collectors.toList());
            System.out.println(list);
        }
    }

    List<Integer> to int[] 

    import java.util.Arrays;
    import java.util.List;
    
    public class Main {
        public static void main(String[] args) {
            List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
            int[] arr = list.stream().mapToInt(Integer::intValue).toArray();
            System.out.println(Arrays.toString(arr));
        }
    }
    

    char[] to String

    public class Main {
        public static void main(String[] args) {
            var c = new char[]{'a', 'b', 'c'};
            var s1 = String.valueOf(c);
            System.out.println(s1);
            var s2 = String.copyValueOf(c);
            System.out.println(s2);
            // valueOf와 copyValueOf는 같다. 
        }
    }

    String to char[]

    public class Main {
        public static void main(String[] args) {
            var s = "abcdefg";
            char[] c = s.toCharArray();
            System.out.println(c);
        }
    }

     

    반응형
Designed by Tistory.