I wanted to make a type safe array slice method (at least for single
dimension arrays)
Here is what I have
public static <T> T [] arraySlice(T [] src, int offset, int length) {
int [] dim = new int [1];
dim[0] = length;
T[] dst;
dst = (T [])
java.lang.reflect.Array.newInstance(src.getClass() .getComponentType() ,
dim );
System.arraycopy(src,offset,dst,0,length);
return dst;
}
This has two problems
1. Eclipse gives a warning for the type cast (T []) , saying
Type safety: The cast from Object to T[] is actually checking against
the erased type Object[]
2. this does not work for array of primitives such as int []
I am assuming this does provide type safe array slicing for single
dimension arrays of non primitives.
My questions :
Is the above a reasonable thing to do ?
Are there better solutions ?
Do you see any other issues with the above code ?
Thanks,
-Antony Sequeira 1 8660
Antony Sequeira wrote: I wanted to make a type safe array slice method (at least for single dimension arrays) Here is what I have
public static <T> T [] arraySlice(T [] src, int offset, int length) { int [] dim = new int [1]; dim[0] = length; T[] dst; dst = (T [])
T [] - "Type variables don’t exist at run time. This means that
they entail no performance overhead in either time nor space,
which is nice. Unfortunately, it also means that you can’t
reliably use them in casts."
,
Leonid
java.lang.reflect.Array.newInstance(src.getClass() .getComponentType() , dim ); System.arraycopy(src,offset,dst,0,length); return dst; }
This has two problems 1. Eclipse gives a warning for the type cast (T []) , saying Type safety: The cast from Object to T[] is actually checking against the erased type Object[] 2. this does not work for array of primitives such as int []
I am assuming this does provide type safe array slicing for single dimension arrays of non primitives.
My questions : Is the above a reasonable thing to do ? Are there better solutions ? Do you see any other issues with the above code ?
Thanks, -Antony Sequeira This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave Bazell |
last post by:
I have a multidimensional array, say 20 x 6, and I want to take a subset of
the rows. I have an index array
@indx=(1,3,12,17) for example, and what I want to say is
@new_array = $data
I know...
|
by: J. J. Cale |
last post by:
I have the following. There must be something less cumbersome without push
pop. The function parameter obj is the event.srcElement and has the
attributes(properties?) picNum and alb pinned to it....
|
by: VK |
last post by:
Whatever you wanted to know about it but always were affraid to ask.
<http://www.geocities.com/schools_ring/ArrayAndHash.html>
|
by: Bryan Olson |
last post by:
The Python slice type has one method 'indices', and reportedly:
This method takes a single integer argument /length/ and
computes information about the extended slice that the slice
object would...
|
by: truckaxle |
last post by:
I am trying to pass a slice from a larger 2-dimensional array to a
function that will work on a smaller region of the array space. The
code below is a distillation of what I am trying to...
|
by: RyanTaylor |
last post by:
I have a final coming up later this week in my beginning Java class and my
prof has decided to give us possible Javascript code we may have to write.
Problem is, we didn't really cover JS and what...
|
by: fahd |
last post by:
Hi,
I'm trying to communicate with an unmanaged c++ dll that takes two 2d
float arrays, one as input with data in it and the other will have the
result of the operation in it as follows:
bool...
|
by: RubyRedRick |
last post by:
I bought Crockford's "JavaScript: The Good Parts" yesterday to help
build my JavaScript foo.
On page 44, he gives an implementation of the curry function:
Function.method('curry', function() {...
|
by: lorlarz |
last post by:
In the code sample below, how are arguments a legitimate
argument to Array.slice?
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object =...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |