toString java of arrays

前端 未结 4 803
旧巷少年郎
旧巷少年郎 2020-12-03 15:20

I have several arrays in a class

I want to implement toString() to print all values.

How to do this?

public String var1[];   
public int var2         


        
4条回答
  •  死守一世寂寞
    2020-12-03 15:36

    String someArray = new String[] {"1", "2"};
    String toString = Arrays.asList(someArray).toString();
    

    The code above will print out the toString in a more readable format:

    [1, 2]

    If you are using JDK 1.5, you can use:

    String[] strings = { "ABC", "DEF" };
    String s = Arrays.toString(strings); 
    

提交回复
热议问题