How to do Arrays.asList on only part of an Object[] array?

Discussion in 'Java' started by laredotornado, Nov 4, 2009.

  1. Hi,

    As you may know, the Arrays.asList method will return an ArrayList
    object from an Object[] array. What is the easiest way to achieve
    this when you only want a specific range of that Object[] array, say,
    its first element up until it's length - 1 element?

    I'm using Java 1.5. Thanks, - Dave
     
    laredotornado, Nov 4, 2009
    #1
    1. Advertising

  2. laredotornado

    Arne Vajhøj Guest

    laredotornado wrote:
    > As you may know, the Arrays.asList method will return an ArrayList
    > object from an Object[] array. What is the easiest way to achieve
    > this when you only want a specific range of that Object[] array, say,
    > its first element up until it's length - 1 element?


    Combining with Arrays.copyOf, but I would write a custom method
    for it.

    Arne
     
    Arne Vajhøj, Nov 4, 2009
    #2
    1. Advertising

  3. laredotornado

    Tom Anderson Guest

    On Wed, 4 Nov 2009, laredotornado wrote:

    > As you may know, the Arrays.asList method will return an ArrayList
    > object from an Object[] array. What is the easiest way to achieve this
    > when you only want a specific range of that Object[] array, say, its
    > first element up until it's length - 1 element?


    Arrays.asList(anArray).subList(startIndex, endIndex);

    Both the array list and the sub-list are lightweight wrappers, so there's
    no copying, just two method calls and some arithmetic on each access.

    tom

    --
    That must be one of the best things you can possibly do with a piglet,
    booze and a cannon. -- D
     
    Tom Anderson, Nov 4, 2009
    #3
  4. laredotornado

    markspace Guest

    laredotornado wrote:
    > Hi,
    >
    > As you may know, the Arrays.asList method will return an ArrayList
    > object from an Object[] array. What is the easiest way to achieve
    > this when you only want a specific range of that Object[] array, say,
    > its first element up until it's length - 1 element?
    >
    > I'm using Java 1.5. Thanks, - Dave



    "copyOfRange", I guess.


    List<X> list = Arrays.asList( Arrays.copyOfRange( x, 0, x.length-1 ));

    Not syntax checked. Note this works a bit differently than asList
    because you use a copy, and the orginal does "write through" like a
    plain asList does.

    I guess you could also do:

    List<X> list = Arrays.asList( x );
    list.remove( list.size()-1 );
     
    markspace, Nov 4, 2009
    #4
  5. laredotornado

    Eric Sosman Guest

    laredotornado wrote:
    > Hi,
    >
    > As you may know, the Arrays.asList method will return an ArrayList
    > object from an Object[] array. What is the easiest way to achieve
    > this when you only want a specific range of that Object[] array, say,
    > its first element up until it's length - 1 element?


    Easiest is probably something like

    List<Foo> all = Arrays.asList(arrayOfFoo);
    List<Foo> some = all.sublist(begin, limit);

    (which you could condense a bit if desired). Alternatively,
    you could use Arrays.copyOfRange() to make a new copy of part
    of the array, and apply Arrays.asList() to that (but changes
    to the copy, of course, would not affect the original).

    --
    Eric Sosman
    lid
     
    Eric Sosman, Nov 4, 2009
    #5
  6. Tom Anderson wrote:
    > On Wed, 4 Nov 2009, laredotornado wrote:
    >
    >> As you may know, the Arrays.asList method will return an ArrayList
    >> object from an Object[] array. What is the easiest way to achieve
    >> this when you only want a specific range of that Object[] array,
    >> say, its first element up until it's length - 1 element?

    >
    > Arrays.asList(anArray).subList(startIndex, endIndex);
    >
    > Both the array list and the sub-list are lightweight wrappers, so
    > there's no copying, just two method calls and some arithmetic on
    > each
    > access.


    Tom took my answer.
     
    Mike Schilling, Nov 4, 2009
    #6
  7. laredotornado

    Lew Guest

    laredotornado wrote:
    > As you may know, the Arrays.asList method will return an ArrayList
    > object from an Object[] array. What is the easiest way to achieve
    > this when you only want a specific range of that Object[] array, say,
    > its first element up until it's length - 1 element?
    >
    > I'm using Java 1.5.


    You have a plethora of answers already, but I'm wondering why you're using an
    obsolete version of Java.

    The RFC for newsgroups specifies that a sig at the end of a message to conform
    to a line with "double-dash space" on its own line, with the sig following on
    subsequent lines.

    There is no requirement to follow that convention, and in fact Google Groups
    (being the sucky trash news reader that it is) eats the space and spoils the
    pattern. However, when you do follow the pattern, good news readers know how
    to deal with it.

    So why are you using an obsolete version of Java?

    --
    Lew
     
    Lew, Nov 5, 2009
    #7
  8. laredotornado

    Roedy Green Guest

    On Wed, 4 Nov 2009 15:10:50 -0800 (PST), laredotornado
    <> wrote, quoted or indirectly quoted someone
    who said :

    >
    >As you may know, the Arrays.asList method will return an ArrayList
    >object from an Object[] array.



    You can feed asList either a template array of the correct type or a
    full template array of just the right size. It is a bit ugly in the
    syntax, but the you end up with an array of the correct type. By
    default, it will create a Object[] so there is not much point in
    providing it an Object[] template.

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    An example (complete and annotated) is worth 1000 lines of BNF.
     
    Roedy Green, Nov 5, 2009
    #8
  9. laredotornado

    Roedy Green Guest

    On Wed, 4 Nov 2009 15:10:50 -0800 (PST), laredotornado
    <> wrote, quoted or indirectly quoted someone
    who said :

    >As you may know, the Arrays.asList method will return an ArrayList
    >object from an Object[] array. What is the easiest way to achieve
    >this when you only want a specific range of that Object[] array, say,
    >its first element up until it's length - 1 element?


    see http://mindprod.com/jgloss/arraylist.html#CONVERTING
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    An example (complete and annotated) is worth 1000 lines of BNF.
     
    Roedy Green, Nov 5, 2009
    #9
  10. On 5 nov, 00:25, Tom Anderson <> wrote:
    > On Wed, 4 Nov 2009, laredotornado wrote:
    > > As you may know, the Arrays.asList method will return an ArrayList
    > > object from an Object[] array.  What is the easiest way to achieve this
    > > when you only want a specific range of that Object[] array, say, its
    > > first element up until it's length - 1 element?

    >
    > Arrays.asList(anArray).subList(startIndex, endIndex);
    >
    > Both the array list and the sub-list are lightweight wrappers, so there's
    > no copying, just two method calls and some arithmetic on each access.
    >


    Be careful though that the returned list is not serializable (which is
    logical, when you think about it). If you intend to send the subList
    to another process, you have to make a copy before.

    JB.
     
    Jean-Baptiste Nizet, Nov 5, 2009
    #10
  11. laredotornado

    Lew Guest

    Christian wrote:
    > Lew schrieb:
    >> So why are you using an obsolete version of Java?
    >>

    >
    > there are people that need their software to work with Mac Os X ...
    > sadly due to Apple's reluctance in not supporting their older
    > OS/Architectures we will all be stuck with Java 5 if we want to be
    > compatible to MacOsX ...
    >
    >
    > so Java 5 even if in EoL will not be obsolete in the next years...


    The OP made no mention of Mac. I don't routinely expand headers, so the
    question was a natural one.

    I thought I'd read that Mac had Java 6 by now.

    As for not being obsolete, Java 5 is no longer available for free from Sun.

    --
    Lew
     
    Lew, Nov 5, 2009
    #11
  12. Lew wrote:
    > laredotornado wrote:
    >> I'm using Java 1.5.

    >
    > You have a plethora of answers already, but I'm wondering why you're
    > using an obsolete version of Java.


    > So why are you using an obsolete version of Java?


    How do you know that he is using an obsolete version of Java?

    SUN's free version EOL'ed October 30.

    But SUN's business version will first EOL June 2019.

    I don't even think IBM has announced a EOL date for
    WAS 6.1 with Java 1.5 yet.

    I will expect Oracle to support whatever WebLogic
    versions using Java 1.5 for quite some time.

    Arne
     
    Arne Vajhøj, Nov 6, 2009
    #12
  13. On Nov 4, 4:25 pm, Tom Anderson <> wrote:
    > On Wed, 4 Nov 2009, laredotornado wrote:
    > > As you may know, the Arrays.asList method will return an ArrayList
    > > object from an Object[] array.  What is the easiest way to achieve this
    > > when you only want a specific range of that Object[] array, say, its
    > > first element up until it's length - 1 element?

    >
    > Arrays.asList(anArray).subList(startIndex, endIndex);
    >
    > Both the array list and the sub-list are lightweight wrappers, so there's
    > no copying, just two method calls and some arithmetic on each access.
    >
    > tom
    >
    > --
    > That must be one of the best things you can possibly do with a piglet,
    > booze and a cannon. -- D


    Forgot to update this thread, but since there was consensus on Tom's
    solution I tried it out and it worked great. 5 stars, - Dave
     
    laredotornado, Nov 6, 2009
    #13
  14. Lew wrote:
    > Christian wrote:
    >> Lew schrieb:
    >>> So why are you using an obsolete version of Java?

    >>
    >> there are people that need their software to work with Mac Os X ...
    >> sadly due to Apple's reluctance in not supporting their older
    >> OS/Architectures we will all be stuck with Java 5 if we want to be
    >> compatible to MacOsX ...
    >>
    >> so Java 5 even if in EoL will not be obsolete in the next years...

    >
    > The OP made no mention of Mac. I don't routinely expand headers, so the
    > question was a natural one.
    >
    > I thought I'd read that Mac had Java 6 by now.


    It has. But only newer versions. For whatever reason Apples does
    not backport.

    > As for not being obsolete, Java 5 is no longer available for free from Sun.


    Support is not available.

    It is still available for download.

    Arne
     
    Arne Vajhøj, Nov 9, 2009
    #14
  15. laredotornado

    Arne Vajhøj Guest

    laredotornado wrote:
    > On Nov 4, 4:25 pm, Tom Anderson <> wrote:
    >> On Wed, 4 Nov 2009, laredotornado wrote:
    >>> As you may know, the Arrays.asList method will return an ArrayList
    >>> object from an Object[] array. What is the easiest way to achieve this
    >>> when you only want a specific range of that Object[] array, say, its
    >>> first element up until it's length - 1 element?

    >> Arrays.asList(anArray).subList(startIndex, endIndex);
    >>
    >> Both the array list and the sub-list are lightweight wrappers, so there's
    >> no copying, just two method calls and some arithmetic on each access.

    >
    > Forgot to update this thread, but since there was consensus on Tom's
    > solution I tried it out and it worked great. 5 stars, - Dave


    Just note that as Tom wrote then asList and subList does not copy
    data.

    Besides meaning good performance it also means that modifications
    to the list also affects the array.

    import java.util.Arrays;
    import java.util.List;

    public class Backing {
    public static void main(String[] args) {
    String[] sa = { "A", "BB", "CCC", "DDDD" };
    List<String> sl = Arrays.asList(sa).subList(1, 3);
    sl.set(0, "BBX");
    sl.set(1, "CCCX");
    for(String s : sa) {
    System.out.println(s);
    }
    }
    }

    outputs:

    A
    BBX
    CCCX
    DDDD

    That is fine.

    You just need to be aware of it.

    Arne
     
    Arne Vajhøj, Nov 9, 2009
    #15
  16. laredotornado

    Lew Guest

    Lew wrote:
    >> As for not being obsolete, Java 5 is no longer available
    >> for free from Sun.


    Arne Vajhøj wrote:
    > Support is not available.
    >
    > It is still available for download.


    Do you have a link for a free download of Java 5 from Sun?

    --
    Lew
     
    Lew, Nov 9, 2009
    #16
  17. laredotornado

    Lew Guest

    Lew wrote:
    >>> As for not being obsolete, Java 5 is no longer available for free
    >>> from Sun.


    Arne Vajhøj wrote:
    >> Support is not available.
    >>
    >> It is still available for download.


    Lew wrote:
    > Do you have a link for a free download of Java 5 from Sun?


    I finally found it:
    <http://java.sun.com/javase/downloads/index_jdk5.jsp>

    --
    Lew
     
    Lew, Nov 9, 2009
    #17
  18. laredotornado

    Lew Guest

    Lew wrote:
    >>>> As for not being obsolete, Java 5 is no longer available for free
    >>>> from Sun.


    Arne Vajhøj wrote:
    >>> Support is not available.
    >>>
    >>> It is still available for download.


    Lew wrote:
    >> Do you have a link for a free download of Java 5 from Sun?

    >
    > I finally found it:
    > <http://java.sun.com/javase/downloads/index_jdk5.jsp>


    And support is available for a fee.
    <http://www.sun.com/software/javaforbusiness/support.jsp>
    <http://www.sun.com/software/javaforbusiness/getit_download.jsp>

    So Sun's definition of "End of Service Life" is a bit loose.

    --
    Lew
     
    Lew, Nov 9, 2009
    #18
  19. Lew wrote:
    > Lew wrote:
    >>>>> As for not being obsolete, Java 5 is no longer available for free
    >>>>> from Sun.

    >
    > Arne Vajhøj wrote:
    >>>> Support is not available.
    >>>>
    >>>> It is still available for download.

    >
    > Lew wrote:
    >>> Do you have a link for a free download of Java 5 from Sun?

    >>
    >> I finally found it:
    >> <http://java.sun.com/javase/downloads/index_jdk5.jsp>

    >
    > And support is available for a fee.
    > <http://www.sun.com/software/javaforbusiness/support.jsp>
    > <http://www.sun.com/software/javaforbusiness/getit_download.jsp>
    >
    > So Sun's definition of "End of Service Life" is a bit loose.


    Not really.

    They have a free version that they EOL relative early and
    a for money version they support a lot longer.

    It makes sense to me.

    Arne
     
    Arne Vajhøj, Nov 9, 2009
    #19
  20. Lew wrote:
    > Lew wrote:
    >>>> As for not being obsolete, Java 5 is no longer available for free
    >>>> from Sun.

    >
    > Arne Vajhøj wrote:
    >>> Support is not available.
    >>>
    >>> It is still available for download.

    >
    > Lew wrote:
    >> Do you have a link for a free download of Java 5 from Sun?

    >
    > I finally found it:
    > <http://java.sun.com/javase/downloads/index_jdk5.jsp>


    You can still download Java 1.1.8 if you want to.

    http://java.sun.com/products/archive/

    Arne
     
    Arne Vajhøj, Nov 9, 2009
    #20
    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. Alexandra Stehman
    Replies:
    5
    Views:
    30,659
    Chris Smith
    Jun 17, 2004
  2. Adam Monsen
    Replies:
    3
    Views:
    663
    Adam Monsen
    May 20, 2007
  3. Ken
    Replies:
    35
    Views:
    2,256
  4. Alex J
    Replies:
    4
    Views:
    684
    Arne Vajhøj
    Jul 22, 2011
  5. Roedy Green
    Replies:
    2
    Views:
    133
    Stanimir Stamenkov
    May 10, 2014
Loading...

Share This Page