Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toString java of arrays

Tags:

java

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[];
public String var3[][];
public int var4[];
public int var5[][];

public String toString() {

        for(String s :  var1) {
              System.out.println(s.toString());           
        }


        return  null;
    }

That prints all var1[] content but how to print all?? Do I have to put a loop for every one of them?

like image 776
edgarmtze Avatar asked Mar 24 '11 06:03

edgarmtze


2 Answers

You can use the Arrays.toString() static helper method as follows:

String lines[] = getInputArray();
System.out.println(java.util.Arrays.toString(lines));
like image 63
Adrian Petrescu Avatar answered Oct 10 '22 12:10

Adrian Petrescu


I think what you are looking for is Arrays.deepToString()

Refer to this link for more details. It takes an array and calls toString() on every element.

like image 22
Nodebody Avatar answered Oct 10 '22 11:10

Nodebody