Array Slices in Java

Discussion in 'Java' started by kvnsmnsn@hotmail.com, May 28, 2005.

  1. Guest

    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_
    , May 28, 2005
    #1
    1. Advertising

  2. Lucy Guest

    <> wrote in message
    news:...
    > 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.
    Lucy, May 28, 2005
    #2
    1. Advertising

  3. Lee Weiner Guest

    In article <>, 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
    Lee Weiner, May 28, 2005
    #3
  4. Guest

    "Lee Weiner" <> wrote in message
    news:29Ole.15332$g66.10788@attbi_s71...
    > In article <>,
    > 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.
    , May 28, 2005
    #4
  5. Filip Larsen Guest

    <> 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
    Filip Larsen, May 28, 2005
    #5
  6. Lucy coughed up:
    > <> wrote in message
    > news:...
    >> 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,wherethehell'smypen???"
    Thomas G. Marshall, May 29, 2005
    #6
  7. Filip Larsen coughed up:
    > <> 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,wherethehell'smypen???"
    Thomas G. Marshall, May 29, 2005
    #7
  8. Lucy Guest

    "Thomas G. Marshall" <>
    wrote in message news:NXjme.6686$Fb.4425@trndny07...
    > Lucy coughed up:
    > > <> wrote in message
    > > news:...
    > >> 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.
    Lucy, May 30, 2005
    #8
  9. Lucy coughed up:
    > "Thomas G. Marshall"
    > <> wrote in
    > message news:NXjme.6686$Fb.4425@trndny07...
    >> Lucy coughed up:
    >>> <> wrote in message
    >>> news:...
    >>>> 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..."
    Thomas G. Marshall, May 30, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. proudfeet

    array slices

    proudfeet, Oct 1, 2003, in forum: VHDL
    Replies:
    1
    Views:
    604
    Egbert Molenkamp
    Oct 1, 2003
  2. Alex Polite

    No Array slices! What is this?

    Alex Polite, Jun 6, 2004, in forum: Java
    Replies:
    3
    Views:
    505
    John C. Bollinger
    Jun 7, 2004
  3. Patrick Tyler

    string/array slices

    Patrick Tyler, Mar 30, 2011, in forum: Ruby
    Replies:
    12
    Views:
    259
    Adam Prescott
    Mar 31, 2011
  4. drKreso
    Replies:
    5
    Views:
    591
    drKreso
    Jan 5, 2012
  5. TonyShirt

    traversing array slices

    TonyShirt, Feb 27, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    157
    valued customer
    Feb 28, 2004
Loading...

Share This Page