tubby
tubby

Reputation: 2144

One liner to create array list which shows initial size to be 100

I would like to create an array list such that when I print ArrayList.size(), I want it to display 100 or some other large number.

Scenario It's for a program where I'm trying to find the minimum size of array list that fits a condition. So I want the initial value of the ArrayList to be large, so that it keeps getting over-written by a smaller combination.

Similarly to a program to find the smallest number where you initialize the variable smallest to a large number so that it gets over-written in a linear traversal.

EDIT : This is the question I am trying to solve To find the minimum number of coins of denomination 3 and 5 to make change of 4. Yes, it should basically return that it's not possible.

I have done this correctly if the question is only to find the number of coins

public class FindCount
{
    public static void main(String[] args)
    {
        int coins = fun(4);
        if(coins == 999)    
            System.out.println("Not possible to make change with this denomination");
        else 
            System.out.println(coins);
    }

    static int fun(int n)
    {   
        if(n == 0)
            return 0;

        int ret1 = 999;
        int ret2 = 999;

        if(n >= 3)
        {
            int rec1 = fun(n-3);
            if(rec1 != 999) 
                ret1 = 1+rec1;
        }

        if(n >= 5)
        {
            int rec2 = fun(n-5);
            if(rec2 != 999) 
                ret2 = 1+rec2;
        }

        int totalret = min(ret1, ret2);

        return totalret;
    }

    static int min(int a, int b)
    {
        return a<b?a:b;
    }
}

Note that above, I have used a variable called totalret which, if 999, I can use it to declare that it's not possible to achieve this result.

However, if I'm asked to print the elements required instead of just finding count, the program goes wrong if there is any path where it's not possible to make change. This is because, here totalret is initialized to null and not to a large ArrayList of say 999 elements, as in the program above.

import java.util.ArrayList;


public class PrintSequence
{
    static int[] options = {3,5};

    public static void main(String[] args)
    {
        ArrayList<Integer> result = new ArrayList<Integer>();
        result = fun(4);
        for(int i = 0;i<result.size();i++)
            System.out.print(result.get(i));
    }

    static ArrayList<Integer> fun(int n)
    {       
        int smallest = 999;
        ArrayList<Integer> totalret = null;
        for(int i = 0;i<options.length;i++)
        {
            if(n-options[i] >= 0)
            {
                ArrayList<Integer> reci = fun(n-options[i]);
                ArrayList<Integer> reti = new ArrayList<Integer>();
                reti.addAll(reci);
                reti.add(0,options[i]);

                if(reti.size() < smallest)
                {
                    totalret = reti;
                    smallest = reti.size();
                }
            }
        }

        if(totalret == null)
            totalret = new ArrayList<Integer>();

        return totalret;
    }
}

Please suggest how I can edit the printing program to print that this is not possible.

Upvotes: 2

Views: 440

Answers (4)

Pritam Banerjee
Pritam Banerjee

Reputation: 18923

This might help you, though it is not a one liner.

Integer[] intArray = new Integer[100]; 
ArrayList arr  = new ArrayList<Integer>(Arrays.asList(intArray));
int size = arr.size();
System.out.println(size);

Upvotes: 2

Andreas
Andreas

Reputation: 159124

One-liner to create an ArrayList of size 100:

new ArrayList<Object>(Arrays.asList(new Object[100]))

Update

Replace both Object with any suitable (non-primitive) type.

If you don't need it to be an ArrayList, but just a List that cannot change size, the following is good (showing for String as an example):

Arrays.asList(new String[100])

Upvotes: 2

DhruvG
DhruvG

Reputation: 414

Hope this is can help you.

Integer[] is = new Integer[100 + new Random().nextInt(500)];
Arrays.fill(is, 0);
List<Integer> list = Arrays.asList(is);

Upvotes: -1

cottonman
cottonman

Reputation: 404

Hmmm. If you have to use an ArrayList, the only way that I can think of initializing its size would be to fill it up with "dummy" values i.e. loop and add some value up to a certain Nth size. Would that be okay/feasible in your case?

Of course, if anyone else has a better solution, feel free to comment.

Upvotes: 0

Related Questions