Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to do Arrays.asList on only part of an Object[] array?

Reply
Thread Tools

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

 
 
laredotornado
Guest
Posts: n/a
 
      11-04-2009
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
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      11-04-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?


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

Arne
 
Reply With Quote
 
 
 
 
Tom Anderson
Guest
Posts: n/a
 
      11-04-2009
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
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      11-04-2009
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 );
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-04-2009
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      11-04-2009
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.


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-05-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?
>
> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-05-2009
On Wed, 4 Nov 2009 15:10:50 -0800 (PST), laredotornado
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-05-2009
On Wed, 4 Nov 2009 15:10:50 -0800 (PST), laredotornado
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Jean-Baptiste Nizet
Guest
Posts: n/a
 
      11-05-2009
On 5 nov, 00:25, Tom Anderson <(E-Mail Removed)> 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.
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Variable displays at one part while does not in another part in a Jack ASP General 8 05-10-2005 07:26 PM
ActiveX apologetic Larry Seltzer... "Sun paid for malicious ActiveX code, and Firefox is bad, bad bad baad. please use ActiveX, it's secure and nice!" (ok, the last part is irony on my part) fernando.cassia@gmail.com Java 0 04-16-2005 10:05 PM
Easy part done, now the hard part!! jollyjimpoppy A+ Certification 0 09-10-2003 10:37 AM



Advertisments