Java追加到函数中ArrayList的末尾

标签 java android arraylist

使用下面的函数我可以创建 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/

相关文章:

java - Kotlin 数据库错误 CursorIndexOutOfBoundsException

java - 重复的对象被添加到列表中

java - java 面向对象连接两个类

java - 一次导入eclipse中的所有导入?

java - Android ListAdapter 启动@top

javascript - 应用程序在键盘弹出窗口上搞砸了

java - 为什么允许 "new ArrayList<E>"?

java - 如何在java中查找arraylist的重复对象

java - 如何读取特定格式的内存映射文件?

java - Maven 3 - 如何添加注释处理器依赖项?