问题描述
我正在尝试将我的数据库信息插入到2个数组(arrayWeight和arrayId)中,两个数组都进入DataPoint数组(取自GraphView库开源代码)和
I'm trying to insert my database information into 2 arrays (arrayWeight and arrayId), and both arrays goes into DataPoint array (taken from GraphView library open source code) and the
new DataPoint(i,j);
将生成值为i和j的新DataPoint。
无论如何,我已经设法让代码使用编译器,但由于某些原因,当我运行应用程序时,我的应用程序崩溃。
这个问题可能是什么原因?
我尝试过:
私有DataPoint [] getDataPoint(){
if(olddb.check()){
List< olddetails> details = olddb.getDetails();
double [] arrayWeight = new double [olddb.getDetailsCount()];
int [] arrayId = new int [olddb.getDetailsCount ()];
for(oldDetails cn:details){//将所有时间的权重和id添加到数组中。
double num = cn.getWeight();
int id = cn.getId();
for(int i = arrayWeight.length; i> 0; i--){
arrayWeight [i] = num;
arrayId [i] = id;
}
}
DataPoint [] dp = new DataPoint [olddb.getDetailsCount()];
for(int i = 0; i< arrayId.length; i ++){
for( int j = 0; j< arrayWeight.length; j ++)
dp [i] = new DataPoint(i,j);
}
返回dp;
}
else {
DataPoint [] dp = new DataPoint [] {
new DataPoint(0,0)
};
返回dp;
}
}
我还有以下代码(可行):
private DataPoint [] getDataPoint(){
DataPoint [] dp = new DataPoint [] {
new DataPoint(0,1),
new DataPoint(2,5),
新DataPoint(5,5),
新DataPoint(7,4)
} ;
返回dp;
}
will make new DataPoint with values of i and j.
Anyway, I've managed to get the code working with compiler but for some reason when I run the application my app crashes.
What could be the cause of this problem?
What I have tried:
private DataPoint[] getDataPoint() {
if (olddb.check()) {
List<olddetails> details = olddb.getDetails();
double[] arrayWeight = new double[olddb.getDetailsCount()];
int[] arrayId = new int[olddb.getDetailsCount()];
for (oldDetails cn : details) { //Adding weights and id of all time to an array.
double num = cn.getWeight();
int id = cn.getId();
for (int i = arrayWeight.length; i > 0; i--) {
arrayWeight[i] = num;
arrayId[i] = id;
}
}
DataPoint[] dp = new DataPoint[olddb.getDetailsCount()];
for (int i = 0; i < arrayId.length; i++) {
for (int j = 0; j < arrayWeight.length; j++)
dp[i] = new DataPoint(i, j);
}
return dp;
}
else {
DataPoint[] dp = new DataPoint[]{
new DataPoint(0, 0)
};
return dp;
}
}
Also I have the following code for example (it works):
private DataPoint[] getDataPoint(){
DataPoint[] dp = new DataPoint[]{
new DataPoint(0,1),
new DataPoint(2,5),
new DataPoint(5,5),
new DataPoint(7,4)
};
return dp;
}
推荐答案
for (int i = arrayWeight.length; i > 0; i--) {
arrayWeight[i] = num;
arrayId[i] = id;
}
数组限制为[0..length -1]。使用 length
的值作为您尝试在数组范围之外访问的索引。这将导致各种问题,包括崩溃您的应用程序。
The array limits are [ 0..length -1]. Using the value of length
as an index you are trying to access outside the bounds of the array. This will cause various problems including crashing your app.
这篇关于将值插入数据点阵列使应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!