Avishek
Avishek

Reputation: 99

How to access a 2D array inside an object?

I am getting an object as a result of a method call. It is a raster/grid/matrix with rows and columns. But the return type of the method is a object.

On debugging, I can see the 2D array inside. But I am not being able to cast it into anything.

Object vPixels = pixelblock3.getPixelDataByRef(0);
Integer[][] wPixels = (Integer[][]) vPixels;

I get a ClassCastException at the 2nd line: [[B cannot be cast to [[Ljava.lang.Integer;

How can I fix this problem?

Upvotes: 2

Views: 110

Answers (2)

pinco
pinco

Reputation: 891

A simple trick to know what is the type of the object:

Object vPixels = pixelblock3.getPixelDataByRef(0);
Class cls = vPixels.getClass();
System.out.println("The type of the object is: " + cls.getName());

Then you can cast to the appropriate type.

EDIT: as Luigi pointed out [[B means byte[][] so his answer is the correct one.

Upvotes: 0

Luigi
Luigi

Reputation: 8847

Just a guess: try:

int[][] wPixels = (int[][]) vPixels; 

Or

byte[][] wPixels = (byte[][]) vPixels; 

I think that [[B, "B" means primitive bytes.

Upvotes: 3

Related Questions