![]() |
|
|
|||||||
![]() |
Java - How to do Arrays.asList on only part of an Object[] array? |
|
|
Thread Tools | Search this Thread |
|
|
#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 |
|
|
|
|
#2 |
|
Posts: n/a
|
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 |
|
|
|
#3 |
|
Posts: n/a
|
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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
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 |
|
|
|
#6 |
|
Posts: n/a
|
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 |
|
|
|
#7 |
|
Posts: n/a
|
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 |
|
|
|
#8 |
|
Posts: n/a
|
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 |
|
|
|
#9 |
|
Posts: n/a
|
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 |
|
|
|
#10 |
|
Posts: n/a
|
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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |