Array Slices in Java

K

kvnsmnsn

In Ada with an array variable you can declare an array slice like so:

ArrayVariable( 3..5)

that behaves in every way like an array in its own right. In Java you
get a similar thing for <Strings>; if <StringObject> is a <String>
then

StringObject.substring( 3, 5)

is also a full-fledged <String>. Is there some way to do array slices
in Java too automatically, or would I have to explicitly write code to
copy the "slice" over from one array to another?

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
L

Lucy

In Ada with an array variable you can declare an array slice like so:

ArrayVariable( 3..5)

that behaves in every way like an array in its own right. In Java you
get a similar thing for <Strings>; if <StringObject> is a <String>
then

StringObject.substring( 3, 5)

is also a full-fledged <String>. Is there some way to do array slices
in Java too automatically, or would I have to explicitly write code to
copy the "slice" over from one array to another?

A String is not an array.
 
L

Lee Weiner

In Ada with an array variable you can declare an array slice like so:

ArrayVariable( 3..5)

that behaves in every way like an array in its own right. In Java you
get a similar thing for <Strings>; if <StringObject> is a <String>
then

StringObject.substring( 3, 5)

is also a full-fledged <String>. Is there some way to do array slices
in Java too automatically, or would I have to explicitly write code to
copy the "slice" over from one array to another?

Take a look at the arraycopy() method in the System class.

Lee Weiner
lee AT leeweiner DOT org
 
F

Filip Larsen

In Ada with an array variable you can declare an array slice like so:

ArrayVariable( 3..5)

that behaves in every way like an array in its own right. In Java you
get a similar thing for <Strings>; if <StringObject> is a <String>
then

StringObject.substring( 3, 5)

is also a full-fledged <String>. Is there some way to do array slices
in Java too automatically, or would I have to explicitly write code to
copy the "slice" over from one array to another?

Java arrays do not directly support slices, but if your component type
is object references (as opposed to a primitive type), you should be
able to use one of the java.util.List implementations with their support
for sublists.


Best regards,
 
T

Thomas G. Marshall

Lucy coughed up:
A String is not an array.

He's using it as an analogy: he said "you get a similar thing for
<Strings>". And also String itself contains an array of characters.
 
T

Thomas G. Marshall

Filip Larsen coughed up:
Java arrays do not directly support slices, but if your component type
is object references (as opposed to a primitive type), you should be
able to use one of the java.util.List implementations with their
support for sublists.


Also as an aside, if it is indeed an array of object references, a copied
array will necessarily have a copy of the references (and not a copy of
objects). He'll need to keep this in mind, because modifying the object
pointed to by the reference in one array slot will of course modify the
object pointed to by the same reference in the other array.

This may or may not mimic the "slicing" behavior he is interested in. The
reason that I am unsure of this is I don't know Ada, and because he mentions
two things:

1. substring(), which returns a new string, but with
the same underlying character array
2. copying the array elements to a new array

Which are different enough notions to keep my answer vague.
 
L

Lucy

Thomas G. Marshall said:
Lucy coughed up:

He's using it as an analogy: he said "you get a similar thing for
<Strings>". And also String itself contains an array of characters.

The fact that the current version ( ok I only looked at 1.4.2 not 5.0)
of the String class uses a char array for storage
is private and you are not supposed to know about it.
 
T

Thomas G. Marshall

Lucy coughed up:
"Thomas G. Marshall"


The fact that the current version ( ok I only looked at 1.4.2 not 5.0)
of the String class uses a char array for storage
is private and you are not supposed to know about it.


Whether you are or are not "supposed to know about it" does not matter. The
op was not asserting that string was an array, hence there was no need for
you to say "A String is not an array". He was asking if there was an array
equivalent to what he saw in strings.

But this subthread is clearly not going anywhere...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top