By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 2,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

separate array into sub-arrays

P: n/a
Hi, all,

If I have an array like
int[] m = new int[] { 0, 1, 2, 3 },
is there a way I can separate the array into two, like
int[] m01 = somefunction?(m,0,2) // m01 will hold 1st and 2nd items
in array m
int[] m23 = somefunction?(m,2,2) // m23 will hold srd and 4th items
in array m ,
and &m01 = &m, &m23 = &m + 2, always, (& is the get address of operation)

In another words, what I want to do is to separate the array into
sub-arrays, and the sub-arrays should reference the same items in the
original array.

Any idea will be appreciated!
Thanks,
Yanhao Zhu
Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
First thing that comes to mind is Array.Copy:

int[] i1 = {1,2,3,4};
int[] i2=new int[2];
int[] i3= new int[2];
Array.Copy(i1,0,i2,0,2);
Array.Copy(i1,2,i3,0,2);

"Yanhao Zhu" <zh*******@hotmail.com> wrote in message
news:Ou**************@tk2msftngp13.phx.gbl...
Hi, all,

If I have an array like
int[] m = new int[] { 0, 1, 2, 3 },
is there a way I can separate the array into two, like
int[] m01 = somefunction?(m,0,2) // m01 will hold 1st and 2nd items in array m
int[] m23 = somefunction?(m,2,2) // m23 will hold srd and 4th items in array m ,
and &m01 = &m, &m23 = &m + 2, always, (& is the get address of operation)

In another words, what I want to do is to separate the array into
sub-arrays, and the sub-arrays should reference the same items in the
original array.

Any idea will be appreciated!
Thanks,
Yanhao Zhu

Nov 15 '05 #2

P: n/a
Hi, Daniel,

My problem is that I don't want to allocate new space for the two sub-arrays
when I separate the original one. When the array size is very large, I think
it is better if the two sub-arrays can just reference the same memory
address as the original one.

Thanks a lot anyway!

Best,
Yanhao Zhu
"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:Mjqmb.10753$ao4.24601@attbi_s51...
First thing that comes to mind is Array.Copy:

int[] i1 = {1,2,3,4};
int[] i2=new int[2];
int[] i3= new int[2];
Array.Copy(i1,0,i2,0,2);
Array.Copy(i1,2,i3,0,2);

"Yanhao Zhu" <zh*******@hotmail.com> wrote in message
news:Ou**************@tk2msftngp13.phx.gbl...
Hi, all,

If I have an array like
int[] m = new int[] { 0, 1, 2, 3 },
is there a way I can separate the array into two, like
int[] m01 = somefunction?(m,0,2) // m01 will hold 1st and 2nd

items
in array m
int[] m23 = somefunction?(m,2,2) // m23 will hold srd and 4th

items
in array m ,
and &m01 = &m, &m23 = &m + 2, always, (& is the get address of operation)
In another words, what I want to do is to separate the array into
sub-arrays, and the sub-arrays should reference the same items in the
original array.

Any idea will be appreciated!
Thanks,
Yanhao Zhu


Nov 15 '05 #3

P: n/a
Yanhao Zhu,
It sounds like you want to define a ProxyArray class.

The ProxyArray would be a class that holds a reference to your original
array, plus a start index & length into that array. Then all the methods on
the proxy would validate against this length and adjust by start index to
retrieve & update values on the actual array.

ProxyArray m01 = New ProxyArray(m,0,2) // m01 will hold 1st and 2nd
ProxyArray m23 = New ProxyArray(m,2,2) // m23 will hold srd and 4th

Note the ProxyArray does not hold any of the values, it holds a reference to
the actual array, plus a start index & a length.

I would consider having ProxyArray implement IList, with most methods
delegating to the actual Array.

Of course with Generics this will be much better ;-)

Hope this helps
Jay

"Yanhao Zhu" <zh*******@hotmail.com> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl...
Hi, Daniel,

My problem is that I don't want to allocate new space for the two sub-arrays when I separate the original one. When the array size is very large, I think it is better if the two sub-arrays can just reference the same memory
address as the original one.

Thanks a lot anyway!

Best,
Yanhao Zhu
"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:Mjqmb.10753$ao4.24601@attbi_s51...
First thing that comes to mind is Array.Copy:

int[] i1 = {1,2,3,4};
int[] i2=new int[2];
int[] i3= new int[2];
Array.Copy(i1,0,i2,0,2);
Array.Copy(i1,2,i3,0,2);

"Yanhao Zhu" <zh*******@hotmail.com> wrote in message
news:Ou**************@tk2msftngp13.phx.gbl...
Hi, all,

If I have an array like
int[] m = new int[] { 0, 1, 2, 3 },
is there a way I can separate the array into two, like
int[] m01 = somefunction?(m,0,2) // m01 will hold 1st and 2nd

items
in array m
int[] m23 = somefunction?(m,2,2) // m23 will hold srd and 4th

items
in array m ,
and &m01 = &m, &m23 = &m + 2, always, (& is the get address of operation)
In another words, what I want to do is to separate the array into
sub-arrays, and the sub-arrays should reference the same items in the
original array.

Any idea will be appreciated!
Thanks,
Yanhao Zhu



Nov 15 '05 #4

P: n/a
I see what you mean, try to post more clearly, as it stands & means little
in the context of generic managed code.

Anyway, the solution Jay suggested would probably be ideal.
"Yanhao Zhu" <zh*******@hotmail.com> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl...
Hi, Daniel,

My problem is that I don't want to allocate new space for the two sub-arrays when I separate the original one. When the array size is very large, I think it is better if the two sub-arrays can just reference the same memory
address as the original one.

Thanks a lot anyway!

Best,
Yanhao Zhu
"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:Mjqmb.10753$ao4.24601@attbi_s51...
First thing that comes to mind is Array.Copy:

int[] i1 = {1,2,3,4};
int[] i2=new int[2];
int[] i3= new int[2];
Array.Copy(i1,0,i2,0,2);
Array.Copy(i1,2,i3,0,2);

"Yanhao Zhu" <zh*******@hotmail.com> wrote in message
news:Ou**************@tk2msftngp13.phx.gbl...
Hi, all,

If I have an array like
int[] m = new int[] { 0, 1, 2, 3 },
is there a way I can separate the array into two, like
int[] m01 = somefunction?(m,0,2) // m01 will hold 1st and 2nd

items
in array m
int[] m23 = somefunction?(m,2,2) // m23 will hold srd and 4th

items
in array m ,
and &m01 = &m, &m23 = &m + 2, always, (& is the get address of operation)
In another words, what I want to do is to separate the array into
sub-arrays, and the sub-arrays should reference the same items in the
original array.

Any idea will be appreciated!
Thanks,
Yanhao Zhu



Nov 15 '05 #5

P: n/a
Hi, Jay,

Thank you very much! This is what I want. But I am just wondering if there
is way to do this without using the proxy class, say, sub-array is the same
type of original one. :-)
Thanks again! You answer is good enough for me.

Have a good one,
Yanhao Zhu

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:OO**************@TK2MSFTNGP11.phx.gbl...
Yanhao Zhu,
It sounds like you want to define a ProxyArray class.

The ProxyArray would be a class that holds a reference to your original
array, plus a start index & length into that array. Then all the methods on the proxy would validate against this length and adjust by start index to
retrieve & update values on the actual array.

ProxyArray m01 = New ProxyArray(m,0,2) // m01 will hold 1st and 2nd
ProxyArray m23 = New ProxyArray(m,2,2) // m23 will hold srd and 4th

Note the ProxyArray does not hold any of the values, it holds a reference to the actual array, plus a start index & a length.

I would consider having ProxyArray implement IList, with most methods
delegating to the actual Array.

Of course with Generics this will be much better ;-)

Hope this helps
Jay

"Yanhao Zhu" <zh*******@hotmail.com> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl...
Hi, Daniel,

My problem is that I don't want to allocate new space for the two

sub-arrays
when I separate the original one. When the array size is very large, I

think
it is better if the two sub-arrays can just reference the same memory
address as the original one.

Thanks a lot anyway!

Best,
Yanhao Zhu
"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:Mjqmb.10753$ao4.24601@attbi_s51...
First thing that comes to mind is Array.Copy:

int[] i1 = {1,2,3,4};
int[] i2=new int[2];
int[] i3= new int[2];
Array.Copy(i1,0,i2,0,2);
Array.Copy(i1,2,i3,0,2);

"Yanhao Zhu" <zh*******@hotmail.com> wrote in message
news:Ou**************@tk2msftngp13.phx.gbl...
> Hi, all,
>
> If I have an array like
> int[] m = new int[] { 0, 1, 2, 3 },
> is there a way I can separate the array into two, like
> int[] m01 = somefunction?(m,0,2) // m01 will hold 1st and 2nd items
> in array m
> int[] m23 = somefunction?(m,2,2) // m23 will hold srd and 4th items
> in array m ,
> and &m01 = &m, &m23 = &m + 2, always, (& is the get address of

operation)
>
> In another words, what I want to do is to separate the array into
> sub-arrays, and the sub-arrays should reference the same items in the > original array.
>
> Any idea will be appreciated!
>
>
> Thanks,
> Yanhao Zhu
>
>



Nov 15 '05 #6

P: n/a
Yes, Thank you very much!
"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:qTBmb.14873$9E1.69475@attbi_s52...
I see what you mean, try to post more clearly, as it stands & means little
in the context of generic managed code.

Anyway, the solution Jay suggested would probably be ideal.
"Yanhao Zhu" <zh*******@hotmail.com> wrote in message
news:ea**************@TK2MSFTNGP10.phx.gbl...
Hi, Daniel,

My problem is that I don't want to allocate new space for the two

sub-arrays
when I separate the original one. When the array size is very large, I

think
it is better if the two sub-arrays can just reference the same memory
address as the original one.

Thanks a lot anyway!

Best,
Yanhao Zhu
"Daniel O'Connell" <onyxkirx@--NOSPAM--comcast.net> wrote in message
news:Mjqmb.10753$ao4.24601@attbi_s51...
First thing that comes to mind is Array.Copy:

int[] i1 = {1,2,3,4};
int[] i2=new int[2];
int[] i3= new int[2];
Array.Copy(i1,0,i2,0,2);
Array.Copy(i1,2,i3,0,2);

"Yanhao Zhu" <zh*******@hotmail.com> wrote in message
news:Ou**************@tk2msftngp13.phx.gbl...
> Hi, all,
>
> If I have an array like
> int[] m = new int[] { 0, 1, 2, 3 },
> is there a way I can separate the array into two, like
> int[] m01 = somefunction?(m,0,2) // m01 will hold 1st and 2nd items
> in array m
> int[] m23 = somefunction?(m,2,2) // m23 will hold srd and 4th items
> in array m ,
> and &m01 = &m, &m23 = &m + 2, always, (& is the get address of

operation)
>
> In another words, what I want to do is to separate the array into
> sub-arrays, and the sub-arrays should reference the same items in the > original array.
>
> Any idea will be appreciated!
>
>
> Thanks,
> Yanhao Zhu
>
>



Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.