Concatenating elements in an array to a string

前端 未结 19 2277
慢半拍i
慢半拍i 2020-12-08 02:43

I\'m confused a bit. I couldn\'t find the answer anywhere ;(

I\'ve got an String array:

String[] arr = [\"1\", \"2\", \"3\"];

then

19条回答
  •  北海茫月
    2020-12-08 02:58

    Arrays.toString(arr);
    

    output is [1,2,3] and you storing it to your string . and printing it so you get output [1,2,3].

    If you want to get output 123 try this:

    public static void main(String[] args) {
        String[] arr= {"1","2","3"};
        String output ="";
        for(String str: arr)
            output=output+str;
        System.out.println(output);
    
    
    }
    

    Output:

    123
    

提交回复
热议问题