ArrayList Construction Problem

S

steven

Hi,

Can I construct an ArrayList this way:

ArrayList list = new ArrayList( obj1, obj2, ... , objn);

writing a mutiple lines of .add(...) is boring!


Thanks in advance.
 
A

Andy Fish

how about:
Object[] things = { obj1, obj2, obj3, ... objn };
ArrayList list = new ArrayList( Arrays.asList(things));

Obviously if all you want is a list and not specifically an ArrayList you
can get away with
ArrayList list = Arrays.asList(things);

it's a bit clunky and not too efficient but I think it's as close as I can
get.

I find most languages are really weak on specifying literals for data
structures. bring back the old 'read' statement in BASIC I say.
 
I

Illya Kysil

Hello, steven!
You wrote on 11 Nov 2003 02:36:17 -0800:

s> Hi,

s> Can I construct an ArrayList this way:
s> ArrayList list = new ArrayList( obj1, obj2, ... , objn);
s> writing a mutiple lines of .add(...) is boring!

Why not?
ArrayList list = new ArrayList(Arrays.asList(new Object[]{obj1, obj2, ... , objn}));

Regards
Illya Kysil, software developer
Delphi/C/C++/C#/Java/Forth/Assembler
If it is NOT SOURCE, it is NOT SOFTWARE. (C) NASA
 
T

Tor Iver Wilhelmsen

ArrayList list = new ArrayList( obj1, obj2, ... , objn);

No, Java doesn't support variable argument lists until 1.5 which isn't
properly released yet.

But there is a constructor which lets you do what you want, now go
look for it.
 
C

Chris Uppal

Michael said:
No, of course you can't.

Um, you say that, and I'd like to agree, but given the rate that the Java
language is bloating up these days, I wouldn't be *too* sure. I think that the
Java 1.5 testbed compiler will accept a constructor for ArrayList like:

ArrayList(Object... data)
{
for (Object each : data)
this.add(each);
}

Whether the real Java 1.5 will have a (possibly template-ised) version of that
is another question, but the answer would appear to be down the taste and
discretion of the Java powers-that-be rather than being constrained by the
availability of syntactic sugar.

-- chris
 
C

Chris Riesbeck

Michael Borgwardt said:
No, of course you can't.


The use a friggin LOOP!

Or, if you just need a List that won't be modified, use

Arrays.asList(new Object[]{ obj1, obj2, ... });

If you need a mutable list, pass the above to your ArrayList constructor.
 
J

John C. Bollinger

steven said:
Can I construct an ArrayList this way:

ArrayList list = new ArrayList( obj1, obj2, ... , objn);

No. The constructors available to you are listed in the ArrayList API
documentation.
writing a mutiple lines of .add(...) is boring!

What a shame. Tedium in programming -- who would ever have expected that?

If you like it better, you can do this:

List list = new ArrayList(Arrays.asList(new Object[] {obj1, obj2, obj3}));

Of course, if it doesn't have to be any particular kind of List, and in
particular if the List can be immutable, then you might as well just say

List list = Arrays.asList(new Object[] {obj1, obj2, obj3});


John Bollinger
(e-mail address removed)
 
C

Chris Uppal

John said:
What a shame. Tedium in programming -- who would ever have expected that?

<chuckle>

You shouldn't discourage the OP; I'd guess that, over the years, the distate
for tedium in programming has done as much to improve the state of programming
languages, systems, and tools, as everything else put together.

-- chris
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top