-3

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;  
        }
    }
}

izayoi
  • 129
  • 5
  • 1
    This is not how you use generics, what are you trying to achieve here? – Joakim Danielson Jun 01 '20 at 12:10
  • 1
    Since you know about `Arrays.toString`, why not just not use `Arrays.toString` instead of your `s` method? As in `Arrays.toString(ra)` and `Arrays.toString(ba)`. – Sweeper Jun 01 '20 at 12:26
  • Array.toString() works for all Objects and types. You don't have to cast it. – Eldar B. Jun 01 '20 at 12:50
  • thanks you all comments. – izayoi Jun 01 '20 at 13:07
  • Does this answer your question? [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Johannes Kuhn Jun 01 '20 at 13:09
  • Thanks you all comments. I fixed describe above to make it clearify, I wish. I use function s() to print every thing in every types( @JoakimDanielson ), It maybe normal variable, array or double array which need directly toString(), Arrays.toString() and Arrays.deepToString() for printf(), should change every time when vars changed. ( Sweeper ) . As you see, parameter for s() is T not T[], so I have to use cast ( Eldar B ) – izayoi Jun 01 '20 at 13:17

1 Answers1

0

If you really want to customize your printing for variable arrays then why not do the following:

    public static String s(Object s) {
        return s.toString();
    }
    public static String s(Object[] s) {
        return Arrays.toString(s);
    }
    public static String s(Object[][] s) {
         return Arrays.deepToString(s);
    }
    // For each primitive type of your single or multi dimension
    // declare the following for either single or multi dimensional
    public static String s(int[][] s) {
        return Arrays.deepToString(s);
    }
    public static String s(int[] s) {
        return Arrays.toString(s);
    }

But keep in mind you are basically repackaging what the API already does.

WJS
  • 36,363
  • 4
  • 24
  • 39
  • I expected a way in several lines, maybe some syntax or API I dont know, or may reflect sth. but looks impossible. – izayoi Jun 01 '20 at 23:25
  • The Java API essentially does what I did above. The big problem is with primitive arrays. So each `Arrays.toString()` method for primitives needs to be overloaded. – WJS Jun 01 '20 at 23:59