Android 中的矩阵初始化?
我尝试在 android 中进行简单的矩阵初始化,但出现错误:java.lang.ArrayIndexOutOfBoundsException
。我正在尝试这个:
Integer id = Integer.valueOf(idcateg);
System.out.println("Id-ul e"+id);
vot = new Integer[sirid.length][id];
for (int i = 0; i < sirid.length; i++) {
vot[i][id] = 0;
}
where id is a value between 1 and 5,sirid.length is a number that reflects number of images from different categorys. For example,I want for category 1 to have something like this :
vot[0][1]=0;
vot[1][1]=0;
vot[2][1]=0;
...等等
我的错误在哪里?
I'm tring to do a simple matrix initialization in android and I get Error: java.lang.ArrayIndexOutOfBoundsException
. I'm trying this:
Integer id = Integer.valueOf(idcateg);
System.out.println("Id-ul e"+id);
vot = new Integer[sirid.length][id];
for (int i = 0; i < sirid.length; i++) {
vot[i][id] = 0;
}
where id is a value between 1 and 5,sirid.length is a number that reflects number of images from different categorys. For example,I want for category 1 to have something like this :
vot[0][1]=0;
vot[1][1]=0;
vot[2][1]=0;
...etc
Where is my mistache?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这个
数组索引从0开始
你通过id将vot数组的大小设置为sirid.lengh但数组起始索引值从0到(size)(不包括size值)请参见for循环
try this
array index start from 0
you set the size of the vot array to sirid.lengh by id but the array start index value from 0 to (size)(size value not included) see the for loop
我想是因为你在 id 上初始化了数组。
之后你调用vot[i][id],它总是太高1。
fe
如果你创建新的 int[3][3]
你只能调用位置 0,1 和 2
祝你好运
I think because you initialize the array on id.
After that you call to vot[i][id] which then will alway be 1 too high.
f.e.
if you create new int[3][3]
you can only call positions 0,1 and 2
good luck
这是因为
id
已被视为字符串的长度,并且您尝试访问相同的元素...但最后一个元素是vat[i][id-1] 但你试图获取
vat[i][id]
所以最好使用这个..
That is because
id
has been considered as Length of the String and you r trying to access the same element... but the last element isvat[i][id-1]
but u r trying to getvat[i][id]
So better use this..