使用下面的函数我可以创建 ArrayList
并将任何值附加到其中。这个 Action 不是问题,但每次调用后我都无法附加到 ArrayList
的末尾没有明确的当前值。每次通话后ArrayList
清楚。
public List<ReceiveFields> getReceivedSMS(long idToDown, long count) throws TException {
.
.
.
String str = WSDLHelper.call(request);
String[] strings = WSDLHelper.convert2(WSDLHelper.convert1(str));
List<ReceiveFields> receiveArray = new ArrayList<ReceiveFields>();
if (strings != null) {
for (int i = 0; i <= strings.length - 1; i++) {
String[] str1 = WSDLHelper.convert3(strings[i]);
try {
receiveArray.add(new ReceiveFields(
Long.valueOf(str1[0]),
str1[1],
str1[2],
URLDecoder.decode(str1[3], "UTF-8"),
URLDecoder.decode(str1[4], "UTF-8"),
WSDLHelper.convertDate(str1[5])));
}
catch (UnsupportedEncodingException ex) {
throw new TException(PublicErrorList.NOT_EXIST_ERROR_DETAIL);
}
}
}
return receiveArray;
}
最佳答案
List<ReceiveFields> allReceiveFields = new ArrayList<>();
...
List<REceiveFields> addition = getReceivedSMS(idToDown, count);
allReceiveFields.addAll(addition);
或者
public void addReceivedSMS(List<ReceiveFields> receiveArray,
long idToDown, long count) throws TException {
List<ReceiveFields> allReceiveFields = new ArrayList<>();
addReceivedSMS(allReceiveFields , idToDown, count);
<小时/>
评论反馈后:
private List<ReceiveFields> rows = new ArrayList<>(); // Was not initialized
private void getR...SMS(...) {
tsms = new ...;
try {
List<ReceiveFields> additions = tsms.getReceivedSMS(start, count);
if (!additions.isEmpty()) { // Maybe extra check
rows.addAll(additions);
getLastReceivedSMSID = rows.get(rows.size() - 1).getLastId();
...
关于Java追加到函数中ArrayList的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25563604/