ArrayIndexOutofBoundException - Java - Android - 接收流数据并以多维数组Java存储

时间:2014-04-04 23:35:06

标签: java android arrays bluetooth indexoutofboundsexception

我通过蓝牙实时接收30000个字符串,大约每秒1000个字符串。我正在将数据解析为double并尝试将这30000个数据点存储到多维array[30000][1]中。但是,当我运行Android应用程序时,由于ArrayIndexOutofBoundException,它会在几点之后崩溃。

我确实意识到这可以在一个奇异的数组中完成,但我最终会增加数组的宽度。

存储的数组大小= [30000][1]

转换的数组大小= [1]

BluetoothChat

case MESSAGE_READ:
    for (int a = 0; a < 30000; a++) {
        byte[] readBuf = (byte[]) msg.obj;
        try {
            String readMessage = new String(readBuf, 0, msg.arg1);
            mConversationArrayAdapter.add("Voltage: " + readMessage);
            double[] convert = new double[1];

            for (int z = 0; z < 1; z++) {
                convert[z] = Double.parseDouble(readMessage);
            }
            for (int j = 0; j < 1; j++) {
                stored[a][j] = convert[a];
            }
        } catch (NumberFormatException e) {
            System.err.println("NumberFormatException: " + e.getMessage());
        }
    }

    finalValue = new HjorthClass(stored);
    if (finalValue.returnSum() == true) {
        seizureResult.setText("A Seizure has been detected");
    }
    break;

logcat的;

04-04 22:44:17.406: E/AndroidRuntime(14619): FATAL EXCEPTION: main
    04-04 22:44:17.406: E/AndroidRuntime(14619): java.lang.ArrayIndexOutOfBoundsException
    04-04 22:44:17.406: E/AndroidRuntime(14619):    at com.example.android.BluetoothChat.BluetoothChat$2.handleMessage(BluetoothChat.java:311)
    04-04 22:44:17.406: E/AndroidRuntime(14619):    at android.os.Handler.dispatchMessage(Handler.java:99)
    04-04 22:44:17.406: E/AndroidRuntime(14619):    at android.os.Looper.loop(Looper.java:130)
    04-04 22:44:17.406: E/AndroidRuntime(14619):    at android.app.ActivityThread.main(ActivityThread.java:3683)
    04-04 22:44:17.406: E/AndroidRuntime(14619):    at java.lang.reflect.Method.invokeNative(Native Method)
    04-04 22:44:17.406: E/AndroidRuntime(14619):    at java.lang.reflect.Method.invoke(Method.java:507)
    04-04 22:44:17.406: E/AndroidRuntime(14619):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    04-04 22:44:17.406: E/AndroidRuntime(14619):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    04-04 22:44:17.406: E/AndroidRuntime(14619):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

首先声明一个大小为1的双数组。

double[] convert = new double[1];

然后你尝试调用该数组 转换[a] ,其中 a 可以是0到2999 ,只要 a&gt; 0 它超出范围。

stored[a][j]= convert[a]; // a > 0 ? if yes then out of bounds

有你的错误。我的猜测应该是:

stored[a][j]= convert[j];