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

L

laredotornado

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
 
A

Arne Vajhøj

laredotornado 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?

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

Arne
 
T

Tom Anderson

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
 
M

markspace

laredotornado said:
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 );
 
E

Eric Sosman

laredotornado said:
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).
 
M

Mike Schilling

Tom 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?

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.
 
L

Lew

laredotornado 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?

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?
 
R

Roedy Green

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.
 
J

Jean-Baptiste Nizet

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.
 
L

Lew

Christian said:
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.
 
A

Arne Vajhøj

Lew said:
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
 
L

laredotornado

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

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
 
A

Arne Vajhøj

Lew said:
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
 
A

Arne Vajhøj

laredotornado 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?
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top