Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

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

 
Thread Tools Search this Thread
Old 11-04-2009, 11:10 PM   #1
Default How to do Arrays.asList on only part of an Object[] array?


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
  Reply With Quote
Old 11-04-2009, 11:22 PM   #2
Arne Vajhøj
 
Posts: n/a
Default Re: How to do Arrays.asList on only part of an Object[] array?
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
  Reply With Quote
Old 11-04-2009, 11:25 PM   #3
Tom Anderson
 
Posts: n/a
Default Re: How to do Arrays.asList on only part of an Object[] array?
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
  Reply With Quote
Old 11-04-2009, 11:26 PM   #4
markspace
 
Posts: n/a
Default Re: How to do Arrays.asList on only part of an Object[] array?
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
  Reply With Quote
Old 11-04-2009, 11:28 PM   #5
Eric Sosman
 
Posts: n/a
Default Re: How to do Arrays.asList on only part of an Object[] array?
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
  Reply With Quote
Old 11-04-2009, 11:50 PM   #6
Mike Schilling
 
Posts: n/a
Default Re: How to do Arrays.asList on only part of an Object[] array?
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
  Reply With Quote
Old 11-05-2009, 01:21 AM   #7
Lew
 
Posts: n/a
Default Re: How to do Arrays.asList on only part of an Object[] array?
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
  Reply With Quote
Old 11-05-2009, 06:03 AM   #8
Roedy Green
 
Posts: n/a
Default Re: How to do Arrays.asList on only part of an Object[] array?
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
  Reply With Quote
Old 11-05-2009, 06:52 AM   #9
Roedy Green
 
Posts: n/a
Default Re: How to do Arrays.asList on only part of an Object[] array?
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
  Reply With Quote
Old 11-05-2009, 09:35 AM   #10
Jean-Baptiste Nizet
 
Posts: n/a
Default Re: How to do Arrays.asList on only part of an Object[] array?
On 5 nov, 00:25, Tom Anderson <t...@urchin.earth.li> 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
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
No array is defined ... John Dean Computer Support 2 03-16-2006 08:02 PM
Re: Stop posting politics at alt.culture.alaska Aratzio Computer Support 102 11-18-2004 01:26 AM
Everytime I try to open OE or IE I get windows installer trying to install office 2003 cteq1@athotmail.com Computer Support 14 11-12-2004 09:00 AM
Applying an image to a new raid array. Michael-NC Computer Information 1 07-21-2004 05:09 AM
Ultra ATA 100/133 Controller configuration? Flippor Computer Support 3 04-02-2004 05:59 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46