0

I want to create an ArrayList<Float> of length 350. I did this:

x = new ArrayList<Float>(350);

No i want this array to have 'zero' float value at each point. i can do this:

for (int i = 0; i< 350 ; i++){
            x.add((float) 0.0);
}

So my question is if there is another way to do the same thing without iterating. I want minimum iterating to increase efficiency.

3
  • O(n) is pretty efficient. You should worry about making other, less efficient areas, more efficient. You cannot initialize an arraylist with default values. The constructor only allows for a cap, nothing, or a collection of values. Commented Jan 9, 2013 at 10:12
  • You can create a float literal by writing 0.0f. That way you don't need to cast it to float. But note that using double instead of float is a good decision in most cases, because almost all Java math functions take and return doubles. When you also use doubles, you avoid unnecessary casting. Commented Jan 9, 2013 at 10:13
  • i am using float because it is smaller than double and takes up smaller space comparatively. I am dealing with lots of huge arrays here so i thought maybe float will be a better choice. Commented Jan 9, 2013 at 10:23

4 Answers 4

7

If you want efficiency I wouldn't use ArrayList or Float here. I wouldn't recommend using float even as it's precision is so poor, unless you really know what you are doing.

I suggest you use an array of double.

double[] x = new double[350]; // all 0.0
Sign up to request clarification or add additional context in comments.

Comments

2

java's Collections class has a nice utility for this: nCopies. Note that this creates an immutable list, but it does exactly what you want :)

4 Comments

The nCopies method also does a loop for the number of elements that you specify, so with regard to "I want minimum iterating to increase efficiency" this doesn't win you anything.
@jlordo It was a guess and you're right, it doesn't loop - but it does return a List which you cannot modify, so that limits its usage.
@soulcheck and Jesper: akaIDIOT stated the fact that it's immutable in the answer...
@jlordo haha looks like i skipped half of it. tl;dr taken to the extreme :)
0

From java documentation:

ArrayList(int initialCapacity) 
          Constructs an empty list with the specified initial capacity.

Note the list is still empty so you have to add the elements one by one. Only capacity is changed.

I would advice you to construct another collection for instance an array and then initialize the list using that constructor:

ArrayList(Collection<? extends E> c) 
          Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Comments

0

As Peter says use an array. If you want this in an array list you can use the code below. This creates an arraylist of 100 integers each with a value of 42

 Integer[] a = new Integer[100];
 Arrays.fill(a, 42);
 List<Integer> l = new ArrayList<Integer>(Arrays.asList(a));
 for (Integer integer : l) {
      System.out.println(integer);
 }

You can modify it easily to be of type Float and assign any initial value you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.