File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to add an array directly to an arrayList? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to add an array directly to an arrayList?" Watch "How to add an array directly to an arrayList?" New topic
Author

How to add an array directly to an arrayList?

Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
Hi,

I'm looking to create an Arraylist of double arrays. Is there any way I can fill it without declaring an array each time? i.e.:



This, of course, does not work, but just trying to give you an idea of what I'm looking for. The other option would be if I could fill a single array and keep adding those, but as far as I can see, Java doesn't allow you to fill an entire array in one line of code unless you are initializing it in the same line.

Does anyone have any suggestions? About the only thing that seems plausible to me now would be to create a new array each time, which seems counter-intuitive.

Thank you very much for your input!!
Terry Tucker
Greenhorn

Joined: Jan 21, 2013
Posts: 22

I am new so my answer is probably no the best way; however, if you do this, it works:
Double[] foo = {39.5, 99.75, 28.73};
values.add(foo);

I don't know why. I am sure someone with much more experience than I can help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61608
    
  67

Why an array of Doubles and not a List?

What is it that you are trying to model?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
Terry, that definitely works but I need to add multiple double arrays to my arrayList, and to do that I'd have to create a new array each time, possible but not ideal.

Bear, I'd prefer to do that but as far as I can tell, items can only be added one at a time, which would be a lot of extra lines of code, or through Arrays.asList, which again requires a new declaration each time. If I'm wrong though, please let me know, thanks! I'm trying to make a polynomial function; each double array contains the coefficients for a polynomial.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18885
    
    8

Well, first of all if you want to add ten arrays to your list then you do actually have to create ten arrays. I don't know why you think that's "not ideal". And it is possible to add them all at once if you like (there's an addAll(Collection) method you could use), but you still have to create them all first. I guess I'm not quite getting what your problem is really.
Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
I just feel like it's a bit of a waste to create 50ish arrays for the sole cause of feeding them into an arrayList. I'll do it if I have to, but I was just wondering if there was an easier way without initializing a new array each time. Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18885
    
    8

I don't see why it's a waste. If you need 50 arrays, then you're going to have to create 50 arrays. I guess I'm still not understanding why that's a problem.

And presumably you wouldn't be creating those arrays solely to put them into the list. That would be pointless, I agree. Presumably you're going to take them out of the list later and use them?
Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
For efficiency I guess? 1 array instead of 50 seems more practical but I'll settle with what I need to, thanks for your input.
lokesh sree
Ranch Hand

Joined: Oct 27, 2009
Posts: 100

Hi,
If you want to initialize the ArrayList with all the arrays at the time of creating the list itself, then you can do it in the following way:


But if you want to keep adding to the list, you have to call the add with the new array intArrList.add(new int[] {7,8,9});

Lokesh
( SCBCD 5, CCENT, SCJP 5 )
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
That will work, but it is ugly syntax. Also declare the variable a List<int[]> not ArrayList.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8196
    
  23

Theresa Marlin wrote:For efficiency I guess?

You'll probably hear this a lot before you stop being a programmer (unlike some of us who are stuck in the rut ).
And if you don't, you haven't talked to the right people:
1. NEVER guess. Never, ever, ever, ever...well, unless you're programming the next Deep Blue.
2. "More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason — including blind stupidity." — W.A. Wulf

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add an array directly to an arrayList?