• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Can you create an ArrayList and have asigned values in one line?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I know I can use the following code:


But I thought I had done this before where I could do something like:


or even:


Can this be done in some way? I am ultimately trying to make a 3 dimensional ArrayList, but I am having trouble with just the first dimension.
I could have sworn I had done this before. Everything I am finding on google is showing the need to add elements individually with .add().
Thank you for any help.
Scribbler
 
Ranch Hand
Posts: 199
4
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you want to use myList.addAll();
 
Steve Skavnicky
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the Reply S Fox.

Could you show me an example?

I just tried:


But it doesn't work.
I also tried [] and {} and ({}). Nothing seems to work.
 
Marshal
Posts: 66637
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

There are several ways you can create and fill a List in one line. Here are a few:-
  • 1: Use the of() method: List<Integer> numbers = List.of(1, 2, 3, 4, 5); Make sure to read the link carefully before trying this.
  • 2: Combine the above with the List's constructor: List<Integer> numbers = new ArrayList<>(List.of(1, 2, 3, 4, 5));
  • 3: Use Arrays#asList(): List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); Again, make sure to read the documentation carefully.
  • 4: Again, combine that with a List constructor: List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
  • 5: Use a Stream. List<Integer> numbers = IntStream.range(1, 6).boxed().collect(Collectors.toList()); Again, please read all the links.
  • 6: Slightly different version with a Stream. List<Integer> numbers = IntStream.of(1, 2, 3, 4, 5).boxed().collect(Collectors.toList()); Again, please read the link. Options 5 and 6 have the advantage that (at least with the present implementation) the runtime type of the value returned is java.util.ArrayList.
  • Please declare your List as List<XYZ> not ArrayList<XYZ>.
     
    Campbell Ritchie
    Marshal
    Posts: 66637
    251
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Steve McSteve wrote:. . . I just tried: . . .addAll(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); . . .
    I also tried [] and {} and ({}). Nothing seems to work.

    Stop guessing about the syntax and read the documentation for the methods. I am not convinced that you can get addAll() to work as you want it.
     
    Steve Skavnicky
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The documentation seems to mention this will make the list "unmodifiable".
    I was wanting to keep things mutable.

    I have been doing python for too long. I can't even understand Java anymore.
    I can't believe I have spent hours on this and still can't make this work.

    Thanks for the replies everyone. This is a lost cause.

    [Edit: remove expletive]
     
    Campbell Ritchie
    Marshal
    Posts: 66637
    251
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Steve Skavnicky wrote:The documentation seems to mention this will make the list "unmodifiable".
    I was wanting to keep things mutable.

    That is what I was hoping you would find. Options 2 4 5 6 create an ArrayList which is mutable after ceation.

    . . . . This is a lost cause.

    What makes you think that? I have given you six suggestions which will do what you were asking for.
    By the way: there is no such thing as a 3D list. There is however such a thing as a List<List<List<XYZ>>>. What would you want such a complicated data structure for?
     
    S Fox
    Ranch Hand
    Posts: 199
    4
    Eclipse IDE C++ Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i'm a little new to java myself so i'm usually doing it something like this:

    list1.addAll(Arrays.asList(1, 2, 3, 4, 5)); // add these specific things to list1
    or
    list1.addAll(list2); // add everything from list2 into list1

    the best and most modern way to do it is probably with streams and lambdas?
     
    Campbell Ritchie
    Marshal
    Posts: 66637
    251
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Both your suggestions will work, but can you squeeze those addAll() calls into one line?
     
    Bartender
    Posts: 3674
    151
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote: List<Integer> numbers = IntStream.of(1, 2, 3, 4, 5).boxed().collect(Collectors.toList());


    You can do without the 'boxed' by using

    But there are more ways. For instance, to get a List(1, 2, 3...,10) you can do:

    or you can do

    and to get the values of 2^x

     
    Campbell Ritchie
    Marshal
    Posts: 66637
    251
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well done, Piet, finding more suggestions
     
    What's wrong? Where are you going? Stop! Read this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!