当尝试填充一个float数组时,ArrayStoreException [英] ArrayStoreException when trying to fill a float array

查看:224
本文介绍了当尝试填充一个float数组时,ArrayStoreException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  float [] [] foo_array = new float [宽度] [HEIGHT]; //假定WDITH和Height定义为
java.util.Arrays.fill(foo_array,Float.POSITIVE_INFINITY);

正如你所看到的,我只是试图在无穷大初始化浮点数组,但这是导致以下异常:

 线程中的异常LWJGL应用程序com.badlogic.gdx.utils.GdxRuntimeException:java.lang.ArrayStoreException :java.lang.Float 
at com.badlogic.gdx.backends.lwjgl.LwjglApplication $ 1.run(LwjglApplication.java:113)
导致:java.lang.ArrayStoreException:java.lang.Float
在java.util.Arrays.fill(Arrays.java:2170)

当然我可以遍历整个数组并将每个值设置为无穷大,我知道这是填充方法(还有什么工作)。但我只是好奇为什么这不起作用,这是什么例外。



编辑:我省略了大部分的异常消息,因为我没有想要这么长时间,并没有提供任何相关信息。

解决方案

根据 JLS 10.5


如果正在分配的值的类型与组件类型不兼容(§5.2),则抛出ArrayStoreException。


似乎你的 distMap 引用不是一个 float [] ,可能是一个 float [] [] ,这将不起作用,因为 float [] 不等同于 float [] []



尝试使用正确的参数,如 foo_array [0] / code>,它的作品:

  float [] [] foo_array = new float [10] [10] //假定WDITH和Height定义为
java.util.Arrays.fill(foo_array [0],Float.POSITIVE_INFINITY);
System.out.println(Arrays.toString(foo_array));

还要查看 fill(float [] a,
float val)



您需要遍历 foo_array 并设置每个 foo_array [i] 。 (float [] floatArrays:foo_array){
java.util.Arrays.fill(floatArrays, Float.POSITIVE_INFINITY);
}

这是一个很好的多维数组教程




Why does the following code raise an exception and what does it mean?

float[][] foo_array = new float[WIDTH][HEIGHT]; //Assume WDITH and Height are defined
java.util.Arrays.fill(foo_array, Float.POSITIVE_INFINITY);

As you can see, I'm just trying to initialize the float array at infinity but this is causing the following exception:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.ArrayStoreException: java.lang.Float
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:113)
Caused by: java.lang.ArrayStoreException: java.lang.Float
at java.util.Arrays.fill(Arrays.java:2170)

Of course I can just iterate through the whole array and set each value to infinity, and I'm aware that's what the fill method does anyway (how else could it work). But I'm just curious about why this doesn't work and what is this exception.

EDIT: I omitted a big part of the exception message as I didn't want to make this so long and it wasn't providing any relevant information.

解决方案

As per JLS 10.5

If the type of the value being assigned is not assignment-compatible (§5.2) with the component type, an ArrayStoreException is thrown.

It seems that your distMap reference is not a float[] , may be a float[][] which will not work , because float[] is not equivalent to float[][].

Try with a correct parameter like foo_array[0] , it works:

float[][] foo_array = new float[10][10]; //Assume WDITH and Height are defined
java.util.Arrays.fill(foo_array[0], Float.POSITIVE_INFINITY);
System.out.println(Arrays.toString(foo_array));

Also look at the method signature of fill(float[] a, float val).

You need to iterate through foo_array and set each foo_array[i] . Sample :

for(float[] floatArrays:foo_array) {
    java.util.Arrays.fill(floatArrays, Float.POSITIVE_INFINITY);
}

Here is a nice tutorial on multi-dimensional arrays.

这篇关于当尝试填充一个float数组时,ArrayStoreException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆