[英]App crashes when adding to an ArrayList in a for loop after first succeeful add
Still new to android and java but getting there, here is my problem. android 和 java 仍然是新手,但到达那里,这是我的问题。
The crash occurs at this point fieldsA.add( A.substring(_start, _a[i]));崩溃发生在此时fieldsA.add( A.substring(_start, _a[i])); it works the first time but not the second time, I do not get an error in my debug window, just a crash.它第一次工作但不是第二次,我在调试 window 中没有收到错误,只是崩溃了。
// number of chars to extract from string value to add to ArrayList. // 要从字符串值中提取的字符数以添加到 ArrayList。
int _a[] = {12,8,8,8,24,24,8,8,4,4,4,5,5,5,7,7,7,7,4,4,4,7,7,7,7,4,8,8,8,1,1,1,1,2}; int _a[] = {12,8,8,8,24,24,8,8,4,4,4,5,5,5,7,7,7,7,4,4,4,7, 7,7,7,4,8,8,8,1,1,1,1,2};
int _b[] = {2,32,32,4,4,4,5,5,5,7,7,7,7,4,4,4,8,1,4,4,4,4,1,4,56,14 }; int _b[] = {2,32,32,4,4,4,5,5,5,7,7,7,7,4,4,4,8,1,4,4,4,4, 1,4,56,14 };
for clarity, _start value is correct and length of string to extract (_a[i]) is also correct, in the case of this crash, _start value is 12 and _a[i] is 8 so the substring(12, 8) should add "12663312" to the ArrayList.为清楚起见,_start 值是正确的,要提取的字符串长度 (_a[i]) 也是正确的,在这次崩溃的情况下,_start 值是 12,_a[i] 是 8,所以 substring(12, 8) 应该将“12663312”添加到 ArrayList。
Question is, what am I doing wrong in how I am using array list, if I was using a vector (which I would normally do in C++) it would be a breeze but this is my first time use of ArratList in java and I cant see what the problem is, maybe I am too old.问题是,我在使用数组列表时做错了什么,如果我使用向量(我通常会在 C++ 中这样做),那将是一件轻而易举的事,但这是我第一次在 java 中使用 ArratList,我不能看看是什么问题,也许我太老了。
Thanks in advance.提前致谢。
public void SetEkmFieldValueStrings(String A, String B)
{
// example string A content
/* $00030000019412663312012914320863457705827487016831160515448803478652000007590515438109551618066451002454245424540005600000000300000984000000000007720001756 100C000 100000003800000000000056000009450020000001600000000000000000232002210030208144700!*/
/* if length of A is less than 250 chars, read is no good, discard
* NOTE: both Strings A and B will need to be validated for checksum
* TODO...
* */
if(A.length() < 250) {return;}
// discard first 4 chars from string, not needed
A = A.substring(4, A.length()-4);
/* array of fields values are NOT empty */
/* clear arrays for new sets of values */
if(fieldsA.size() > 0)
fieldsA.clear();
if(fieldsB.size() > 0)
fieldsB.clear();;
int _start = 0; /* 1st index into field size array */
int i;
for(i = 0; i < _a.length; i++)
{
/* add the substring of String A to the array list of field values
* _a[i] contains the length of string to extract from A
* */
fieldsA.add((String) A.substring(_start, _a[i]));
String j = fieldsA.get(_start); //for testing purpose only
/* set _start to the next field start pont in string A */
_start += _a[i];
}
/* do same for String B*/
_start = 0;
for(i = 0; i < _b.length; i++)
{
/* add the substring of String A to the array list of field values
* _b[i] contains the length in of string to extract
* */
fieldsB.add(B.substring(_start, _b[i]));
/* set _start to the next field start pont in string A */
_start += _b[i];
}
}
Looks to me that You use wrong indexes for substring.在我看来,您对 substring 使用了错误的索引。
Substring method second parameter is not count of chars to take but endIndex . Substring 方法第二个参数不是要获取的字符数,而是endIndex 。
We can find relevant info in javadoc of String.substring:我们可以在String.substring的javadoc中找到相关信息:
public String substring(int beginIndex, int endIndex)
beginIndex - the beginning index, inclusive. beginIndex - 开始索引,包括在内。
endIndex - the ending index, exclusive. endIndex - 结束索引,独占。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.