Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #29319
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Mike Amling <mamling@chaff.us> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: generics array type check |
| Date | Fri, 25 Jul 2014 12:15:54 -0500 |
| Lines | 62 |
| Message-ID | <c3fhltFanmaU1@mid.individual.net> (permalink) |
| References | <lqqekm$mtq$1@ariadne.rz.tu-clausthal.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | individual.net QEmmrpR7lS/NKxEC4ayNswpWrCIsEeTzz2t5CpLzE1gFBugPn1 |
| Cancel-Lock | sha1:7NZ8tj8bzOF7lJX63wx7+csNTEM= |
| User-Agent | Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 |
| In-Reply-To | <lqqekm$mtq$1@ariadne.rz.tu-clausthal.de> |
| Xref | csiph.com comp.lang.java.programmer:29319 |
Show key headers only | View raw
On 7/24/14 3:03 AM, Philipp Kraus wrote:
> Hello,
>
> I have got this signature of a method private static <T> long[] getSize(
> T p_data )
> Depend on the type T the return value is changes e.g. on a non-array type
>
> getSize( 5 ) => new long[1]{1}
> getSize( "test" ) => new long[1]{1}
>
> on an array it should return
>
> double[] p_array
> getSize( p_array ) => new long[1]{ p_data.length }
>
> and on 2-dimensional array it should be
> double[][] p_array
> getSize( p_array ) => new long[2]{ p_array.length, p_array[0].length }
>
> How can I check the generic data type in a correct way to detect the
> array type?
How about something like
static long[] getSize(Object subject) {
if (subject==null) {
return new long[0]; // Or whatever
}
int dimensions=0;
long[] answer={1};
Class klas=subject.getClass();
while (klas.isArray()) {
final long[] more=new long[dimensions+1];
System.arraycopy(answer, 0, more, 0, dimensions);
answer=more;
klas=klas.getComponentType();
if (klas.isPrimitive()) {
if (klas==Byte.TYPE) {
answer[dimensions]=((byte[])subject).length;
} else if (klas==Short.TYPE) {
answer[dimensions]=((short[])subject).length;
...
} else if (klas==Double.TYPE) {
answer[dimensions]=((double[])subject).length;
}
break;
}
final Object[] aray=(Object[])subject;
answer[dimensions++]=aray.length;
subject=aray[0]; // Assumed non-null
}
return answer;
}
and to cover the primitives,
static long[] getSize(final double whatever) {
return new long[]{1};
}
--Mike Amling
QW5kIHRoZXkgc2FpZCBpdCBjb3VsZG4ndCBiZSBkb25lLg==
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
generics array type check Philipp Kraus <philipp.kraus@flashpixx.de> - 2014-07-24 10:03 +0200
Re: generics array type check Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-07-24 09:13 -0400
Re: generics array type check markspace <markspace@nospam.nospam> - 2014-07-24 08:24 -0700
Re: generics array type check Arne Vajhøj <arne@vajhoej.dk> - 2014-07-25 21:14 -0400
Re: generics array type check Mike Amling <mamling@chaff.us> - 2014-07-25 12:15 -0500
Re: generics array type check Mike Amling <mamling@chaff.us> - 2014-07-25 13:21 -0500
Re: generics array type check Arne Vajhøj <arne@vajhoej.dk> - 2014-07-25 21:12 -0400
csiph-web