0

I have an ArrayList ArrayList<int[]> which has one-dimensional array of integers as it's members.

When I am debugging and have my watch set on tmp from the code below, I want to be able to see tmp's contents, which means I have to override the toString method of int[]?

At the moment, I am able to see my contents by printing them out using the print method below. However, I do not want to do that ideally, and would like to see the contents of tmp and it's members from the debugger.

PS : I do not want to have debug watch expressions like, say, tmp.get(0)[0] etc

public class TryPrinting {
    public static void main( String[] args ) {
        int[][] people = { {7,0}, {7,1}, {6,1}, {5,0}, {5,2}, {4,4} };

        TryPrinting obj = new TryPrinting();
        obj.test( people );
    }

    public void test( int[][] people)
    {
        int n = people.length;
        ArrayList<int[]> tmp = new ArrayList<>();
        for (int i = 0; i < n; i++)
        {
            tmp.add(people[i][1], new int[]{people[i][0], people[i][1]});

            print( tmp );//This works
            System.out.println( tmp );//But I want this to work
            System.out.println( "" );
        }

    }

    /* print al */
    public void print( ArrayList<int[]> al )
    {
        for(int i = 0;i<al.size();i++)
        {
            print( al.get( i ) );
        }
        System.out.println( "" );
    }

    /* print 1d array */
    public void print(int[] a)
    {
        for(int i = 0;i<a.length;i++)
            System.out.print( a[i] + " " );
    }
}
3
  • I think you can if you box int to Integer, it becomes an object with a class type, I guess you'll get toString too... Commented Feb 20, 2017 at 0:58
  • The comments on your code implies you're trying to print to console, but your question implies you want your debugger to display it. Which is it? And which IDE are you using? Commented Feb 20, 2017 at 1:02
  • In response to your question title, the answer is No. Commented Feb 20, 2017 at 1:06

1 Answer 1

2

Array classes are final: you cannot extend them and override their methods.

To easily print contents of an array, you can use methods in java.util.Arrays, like this:

System.out.println(Arrays.toString(tmp));

An alternative is to use Lists in place of arrays, which implement a "proper" toString method.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.