• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How to add an array directly to an arrayList?

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Greenhorn
Posts: 24
Postgres Database VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why an array of Doubles and not a List?

What is it that you are trying to model?
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 100
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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});
 
Marshal
Posts: 74376
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That will work, but it is ugly syntax. Also declare the variable a List<int[]> not ArrayList.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic