java - getting a class having the same generic type of another class -
this first question here, make forum lives.
i want image2d class have same generic type of point2d class.
image2d.java :
package utilities; import ij.imageplus; import utilities.point2d; public class image2d<t> { arraylist<point2d<t>> original = new arraylist<point2d<t>>(); public image2d(imageplus imp) { // imp.getprocessor().getintarray() returns int[][] this.original = matrixtoimage2d(imp.getprocessor().getintarray()); } private static <t> arraylist<point2d<t>> matrixtoimage2d(t[][] matrix) { arraylist<point2d<t>> img = new arraylist<point2d<t>>(); (int row=0; row<matrix.length; row++) { (int col=0; col<matrix[row].length; col++) { img.add(new point2d(col, row, matrix[row][col])); } } return img; } }
point2d.java:
package utilities; public class point2d<t> { public int x; public int y; public t value; public point2d(int col, int row, t value) { this.x = col; this.y = row; this.value = value; } public int getx() { return this.x; } public int gety() { return this.y; } public t getvalue() { return this.value; } }
i must not use concept that, put code illustrate idea. please can tell me java concepts have use ?
thanks.
i suspect know problem - you've added comment summarising it.
// imp.getprocessor().getintarray() returns int[][]
the matrixtoimage
method needs t[][]
giving int[][]
not work.
you must either make imageplus
take generic parameter (imageplus<t>
) , make getintarray
return t[][]
or must make matrixtoimage2d
take int[][]
parameter.
one other alternative use t extends number
, make adapter methods translate between int[][]
, t[][]
.
Comments
Post a Comment