0

The following code works in C#:

public void DoSomething(float[][] array){
    for (int i = 0; i < array.GetLength(0); i++)
    {
        int len = array[0].length;
        double[] copy = new double[len];
        Array.Copy(array[i], copy, len);

        ...
    }
}

I'm attempting to convert it into Java. I tried this:

public void DoSomething(float[][] array){
    for (int i = 0; i < array[0].length; i++)
    {
        int len = array[0].length;
        double[] copy = new double[len];
        copy = Arrays.copyOf(array[i], len);

        ...
    }
}

But I get the error message:

Type mismatch: cannot convert from float[] to double[]

What can I do to make it work in Java?

peak
  • 105,803
  • 17
  • 152
  • 177
adam0101
  • 29,096
  • 21
  • 96
  • 174
  • Define "producing different results". Different than *what* exactly? – Bohemian Mar 10 '12 at 08:21
  • Note that it's not casting in C# either. It's a conversion. From the .NET [docs for `Array.Copy(Array, Array, int)`](http://msdn.microsoft.com/en-us/library/k4yx47a1.aspx): "Type compatibility is defined as follows: [...] Two intrinsic (predefined) value types are compatible if copying from the source type to the destination type is a widening conversion. [...] For more information about conversions, see [`Convert.ToDouble(float)`](http://msdn.microsoft.com/en-us/library/kc01y017.aspx)" – Cristian Diaconescu Nov 04 '13 at 13:26

3 Answers3

2

You are trying to assign an array of float to an array of double. Try this:

public void DoSomething(float[][] array) {
    for (int i = 0; i < array[0].length; i++) {
        int len = array[0].length;
        float[] copy = new float[len];  // note change of array type
        copy = Arrays.copyOf(array[i], len);

I can't speak for C#, but java is a strongly typed language, which means you can't do things like what you attempted. While in java a float can be cast to a double, float[] can not be cast to double[].

Patrick
  • 1,717
  • 7
  • 21
  • 28
Bohemian
  • 412,405
  • 93
  • 575
  • 722
2

Because in Java double and float are not the same thing and have different precision. (See: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html)

Edit to add: As for how to make it work, you'd need to do it manually casting each element to double. However, you may have problems with precision; see: Convert float to double without losing precision

Community
  • 1
  • 1
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • Part of my question was why C# allows it. I'm guessing it implicitly casts the arrays. Is that so and is there a way to cast/convert in Java? – adam0101 Mar 09 '12 at 19:57
  • float[] can not be cast to double[] – Bohemian Mar 09 '12 at 19:57
  • @adam0101 - I'm not a C# guy, I'd have to look it up. As I mention, you *can* cast `float` to `double` in java, but the resulting value may not be what you're looking for. The function you're trying to use won't do that for you, you'd have to write the loop yourself and do the cast. The arrays themselves can't just be cast. – Brian Roach Mar 09 '12 at 20:01
-1

Use the

System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

method to copy whole parts of your array into an other array. (JavaDocs)

s1lence
  • 2,188
  • 2
  • 16
  • 34
  • 1
    That approach will compile, but it won't run. From the JavaDoc you link to: "an ArrayStoreException is thrown ... [if] The src argument and dest argument refer to arrays whose component types are different primitive types." – yshavit Mar 09 '12 at 19:52
  • And that's ignoring the fact that if it *did* run you're asking for trouble given the precision differences. – Brian Roach Mar 09 '12 at 19:54
  • @BrianRoach Well, in principle they could check for that and turn it into a standard for loop in which they promote the `float`s to `double`s via the usual rules. But that would defeat the whole purpose of having a native, ultrafast method which is basically just memcpy. – yshavit Mar 09 '12 at 19:56