Posted on 2006-04-24
Last Modified: 2010-03-31
I am aware of the 3 types of ArrayList we have, but is there a way to create an ArrayList myArrayList of  size n and initialize all myArrayList[i] with int 1.( of couse without using a loop. :) )

Question by:komlaaa
    LVL 86

    Expert Comment

    myArrayList = new ArrayList(Arrays.asList(new int[] { 1, 1, 1, 1, 1, 1 }));

    Author Comment

    this approach i good but will be tedious if i size n=1000
    LVL 86

    Assisted Solution

    int[] ints = new int[1000];
    Arrays.fill(ints, 1);

    // (the rest as before)
    LVL 92

    Accepted Solution

    For an ArrayList use:

    ArrayList list = new ArrayList(1000);
    Collections.fill(list, new Integer(1));

    > I am aware of the 3 types of ArrayList we have

    What 3 types are you referring to?
    LVL 86

    Expert Comment


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    array220 challenge 8 32
    strCopies  challenge 17 60
    where is session ID cache stored 1 27
    What is the latest versions eclipse neon 2 60
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now