Felix Crazzolara
Felix Crazzolara

Reputation: 732

How can I change the representation of 2-dimensional arrays in the debugging mode of eclipse?

As you can see in the picture is a variable called board. It's defined as:

int board[][] = new int[10][10] 

I'd like to have a representation like this:

[[3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
[3, 1, 1, 1, 1, 1, 1, 1, 1, 3],
[3, 1, 1, 1, 1, 1, 1, 1, 1, 3],
[3, 1, 1, 2, 1, 1, 1, 1, 1, 3],
[3, 1, 1, 1, 2, 2, 1, 1, 1, 3],
[3, 1, 1, 1, 2, 0, 1, 1, 1, 3],
[3, 1, 1, 2, 1, 1, 1, 1, 1, 3],
[3, 1, 1, 1, 1, 1, 1, 1, 1, 3],
[3, 1, 1, 1, 1, 1, 1, 1, 1, 3],
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]]

Or even better without the brackets and just commas and if possible also that there are arranged like if you'd use:

printf("%3", board[i][j])

To print it as there might be numbers > 10, e.g. with different numbers of characters.

I have seen something very similar some time ago, unfortunately I don't have a screenshot of it.

I have an array

Upvotes: 2

Views: 262

Answers (2)

SHG
SHG

Reputation: 2616

You should encapsulate it in a class and implement its toString() method.

Eclipse has its very basic way to present an array, you cannot change this.

More about toString() here.

Upvotes: 1

BeeOnRope
BeeOnRope

Reputation: 64925

Eclipse has a feature called detail formatters that allow you to write a custom snippet of display code for non-primitive types. Sounds great, right?

Unfortunately, this won't solve your problem directly because it is not applicable for primitive types or arrays. Eclipse always uses its builtin formatters for those types.

You do have a couple of options, however. Perhaps this array is a member of a containing class - in which case you could either add a toString() method which formats the array in the way you'd like, or add a detail formatter for this type, as described above (e.g., because the toString() method may already implemented in a different way that you cannot change).

If that's not the case, the best approach I know of is to still write your formatting method, say String ArrayFormatter.format2D(int[][] array) and then use the Expressions view to make a call to this method with your array, like:

ArrayFormatter.format2D(board)

Then the expressions view will show you the formatted output whenever board is in scope. These expressions persist across debugging runs, so you only have to set this up once.

Upvotes: 1

Related Questions