Array Dimensions

14 views
Skip to first unread message

Tristan Miller

unread,
Jul 31, 2012, 10:45:26 AM7/31/12
to dkpro-l...@googlegroups.com
Greetings.

Could someone explain why the following code doesn't seem to work?

Integer[] foo = new Integer[] {1, 2, 3, 4};
Dimension<Integer[]> bar = Dimension.create("bar", foo);

Eclipse reports the error "Type mismatch: Cannot convert from
Dimension<Integer> to Dimension<Integer[]>"

Regards,
Tristan

--
Tristan Miller
Ubiquitous Knowledge Processing Lab
Department of Computer Science, Technische Universität Darmstadt
Tel: +49 6151 16 6166 | Web: http://www.ukp.tu-darmstadt.de/

signature.asc

Richard Eckart de Castilho

unread,
Jul 31, 2012, 12:59:20 PM7/31/12
to dkpro-l...@googlegroups.com
Hi Tristan,

that is due to the way that Java handles variadic functions. For the compiler the two following methods have the same signature:

void lala(int... values)
void lala(int[] values)

So Java thinks that

Dimension.create("bar", foo);

is equivalent to (and vice versa)

Dimension.create("bar", 1, 2, 3, 4);

You'd have to do something like this:

Dimension.create("bar", new Integer[][] { foo } );

The problem does only occur if you call "create" with one value parameter. This would also work:

Integer[] foo = new Integer[] {1, 2, 3, 4};
Integer[] bar = new Integer[] {4, 3, 2, 1};
Dimension<Integer[]> foobar = Dimension.create("foobar", foo, bar);

Cheers,

-- Richard
Reply all
Reply to author
Forward
0 new messages