I want print single variable, array and maybe double array, I use following code, add a type when I need it, but ... as you see ... it works but not smart.
So, question is "Is there a smart way to print Arrays?"
import java.util.Arrays;
class Rec{
int val;
public String toString(){
return "" + val;
}
Rec(int val){this.val=val;}
}
public class Main
{
public static void main(String[] args) {
Rec r = new Rec(0);
Boolean[] ba = new Boolean[]{true, false, true, true};
Rec[] ra = new Rec[]{new Rec(1), new Rec(2), new Rec(3)};
int[][] iaa = new int[][]{{1,2,3}, {3,4,5}};
System.out.printf("r=%s ba=%s ra=%s iaa=%s \n", s(r), s(ba), s(ra), s(iaa));
}
static <T> String s(T n) {
if(n instanceof int[]){
return Arrays.toString((int[])n);
}else if(n instanceof int[][]){
return Arrays.deepToString((int[][])n);
}else if(n instanceof boolean[]){
return Arrays.toString((boolean[])n);
}else if(n instanceof boolean[][]){
return Arrays.deepToString((boolean[][])n);
}else if(n instanceof Boolean[]){
return Arrays.toString((Boolean[])n);
}else if(n instanceof Rec[]){
return Arrays.toString((Rec[])n);
//}else if(n instanceof T[]){ // error: illegal generic type for instanceof
// return Arrays.toString((T[])n);
}else{
return "" + n;
}
}
}