Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Array Slices in Java

Reply
Thread Tools

Array Slices in Java

 
 
kvnsmnsn@hotmail.com
Guest
Posts: n/a
 
      05-27-2005
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_

 
Reply With Quote
 
 
 
 
Lucy
Guest
Posts: n/a
 
      05-27-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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.


 
Reply With Quote
 
 
 
 
Lee Weiner
Guest
Posts: n/a
 
      05-27-2005
In article <(E-Mail Removed). com>, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>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
 
Reply With Quote
 
gwcherryHatesGreenEggsAndSpam@alum.mit.edu
Guest
Posts: n/a
 
      05-28-2005

"Lee Weiner" <(E-Mail Removed)> wrote in message
news:29Ole.15332$g66.10788@attbi_s71...
> In article <(E-Mail Removed). com>,
> (E-Mail Removed) wrote:
>>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


Boy, are you going to be embarassed.


 
Reply With Quote
 
Filip Larsen
Guest
Posts: n/a
 
      05-28-2005
<(E-Mail Removed)> wrote

> 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,
--
Filip Larsen


 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      05-29-2005
Lucy coughed up:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>> 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.


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.

--
Onedoctortoanother:"Ifthisismyrectalthermometer,wh erethehell'smypen???"



 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      05-29-2005
Filip Larsen coughed up:
> <(E-Mail Removed)> wrote
>
>> 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.



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.


--
Onedoctortoanother:"Ifthisismyrectalthermometer,wh erethehell'smypen???"



 
Reply With Quote
 
Lucy
Guest
Posts: n/a
 
      05-30-2005

"Thomas G. Marshall" <(E-Mail Removed). com>
wrote in message news:NXjme.6686$Fb.4425@trndny07...
> Lucy coughed up:
> > <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) ups.com...
> >> 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.

>
> 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.


 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      05-30-2005
Lucy coughed up:
> "Thomas G. Marshall"
> <(E-Mail Removed). com> wrote in
> message news:NXjme.6686$Fb.4425@trndny07...
>> Lucy coughed up:
>>> <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed) ups.com...
>>>> 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.

>>
>> 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.



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...


--
"His name was Robert Paulson. His name was Robert Paulson. His name was
Robert Paulson..."


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Idiomatic way to get array of possible slices from string drKreso Ruby 5 01-05-2012 10:50 AM
string/array slices Patrick Tyler Ruby 12 03-31-2011 07:17 AM
No Array slices! What is this? Alex Polite Java 3 06-07-2004 01:52 PM
traversing array slices TonyShirt Perl Misc 4 02-28-2004 03:43 AM
array slices proudfeet VHDL 1 10-01-2003 08:55 AM



Advertisments