0

I have a global ArrayList list declared with the intention of adding a float variable to it in a method:

ArrayList<Float[]> list = new ArrayList<Float[]>();

Here is the method:

public void recieve(float[] coords)
{
    this.list.add(?); 
}

What is the syntax for adding coords to the ArrayList?

3
  • I cannot test this at the moment but I am reasonable certain your coords parameter will need to be converted to a Float[] (meaning an array of the object wrapper for a primitive float) before adding it to the ArrayList, since the ArrayList is expecting objects of type Float[], not float[]. Other than that, you have it correct. Commented Nov 27, 2013 at 1:27
  • 1
    I am also not sure that the question is correct. You want to add a float variable, then pass in an array float[]. Commented Nov 27, 2013 at 1:29
  • One will need to change if you want to avoid extra conversion steps. Which one it is depends on external factors: are you using some library or framework that needs one or the other? Are you doing a lot of "real" math that would create a lot of extra boxing and unboxing, slowing things down? Commented Nov 27, 2013 at 1:32

3 Answers 3

4

You will have to convert it manually I think.

public void recieve(float[] coords) {
   this.list.add(convertToFloat(coords));
}

public Float[] convertToFloat(float[] coords) {
  Float[] converted = new Float[coords.length];
  for (int i = 0; i < coords.length; i++) {
     converted[i] = Float.valueOf(coords[i]));
  }
  return converted;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Never use the primitive wrapper constructors: either use the valueOf factory methods or just use autoboxing which does the same thing.
ok i edit it, thanks for the suggestion. What is the reason for that? edit: Ok I just read the javadoc for it which explains it ;)
2

You're looking for this:

public void receive(float[] coords) { // fixed misspelling in name
    Float[] fCoords = new Float[coords.length];
    for (int i = 0; i < coords.length; i++)
        fCoords[i] = coords[i];       // autoboxing takes place here
    this.list.add(fCoords); 
}

Because the ArrayList expects a Float[], but you have a float[] as parameter (notice the difference in letter case!) a manual conversion is required before adding it to the list.

Comments

0

Since you declare Arraylist type as float array, I believe you can just use as

list.add(coords);

Cheers

3 Comments

This will not work because the ArrayList stores Float[] but the method accepts float[]. Each element will need to be converted.
Ah i didn't notice they are different type.. Why don't you make it to accept Float [] then ?
@REALFREE You're technically right, if the rest of the program is designed in such a way that they have the freedom to do that. But we just don't have enough information. They'd probably have to convert it at some point along they way anyways. I suppose they could overload the method too if they are so restricted, to handle either scenario.

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.