0

I have a list as follow:

List<Integer> arrInt={2,3,5};

I want to convert this arraylist to array float.

float[] result={2,3,5};

My code:

    public float[] convertIntToFloat(List<Integer> arr)
    {
      List<Float> arrResult=new ArrayList<Float>();

      for(int i=0;i<arr.size();i++)
      {
        float x=i;
        arrResult.add(x);
      }

      float result[]=arrResult.toArray( new float[arr.size()]);  //Error

      return result;
    }

But it display error in my code as above. Can you help me?

2
  • 3
    What error is it showing? And why are you not directly creating float[] instead of going through ArrayList<Float>? Commented Jul 28, 2013 at 8:19
  • Next time please post the error, it will be much more better. Commented Jul 28, 2013 at 8:25

4 Answers 4

7

List.toArray(T[]) takes an array for Wrapper type, not primitive type.

You should do it like this:

Float result[] = arrResult.toArray( new Float[arr.size()]);  

But, that's really not required. Because, now you would have to convert this to primitive type array. That is too much right?

You can directly create a float[] array, rather than going through an ArrayList<Float>. Just change your arrResults declaration to:

float[] arrResults = new float[arr.size()];

And in for loop, add elements to the array using:

for(int i = 0; i < arr.size(); ++i) {
    arrResults[i] = arr[i];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Here I want to get float[], isn't Float[].
@treehh. float[] is not the same as Float[].
0

Just create the float array directly:

public float[] convertIntToFloat(List<Integer> arr)
{
  float[] arrResult = new float[arr.size()];
  for(int i=0;i<arr.size();i++)
  {
    arrResult[i] = (float) arr[i];
  }

  return arrResult;
}

1 Comment

It's the right idea. It won't compile, but it's the right idea... Edit: I've fixed it for you. You had two arrResult vars (of different types), arr.length where you needed arr.size(), and various missing semicolons.
0

Generic in Java is limited. You can't use (new float[]) as the parameter of toArray. Use (new Float[]) instead. However, in fact, a correct implement should be:

public float[] convertIntToFloat(List<Integer> arr)
{
  float result = new float[arr.size()];

  for(int i = 0; i < arr.size(); ++i)
  {
      result[i] = arr.get(i);
  }

  return result;
}

Comments

0

Do it like this:

public Float[] convertIntToFloat(List<Integer> arr)
    {
      List<Float> arrResult=new ArrayList<Float>();

      for(int i=0;i<arr.size();i++)
      {
        float x=arr.get(i);
//      float x=i;       //Here you are retrieving index instead of value 

        arrResult.add(x);
      }

      Float result[] = arrResult.toArray( new Float[arr.size()]);  
//    float result[]=arrResult.toArray( new float[arr.size()]);  //Error

      return result;
    }

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.