Java在函数中附加到ArrayList的末尾

时间:2014-08-29 07:32:07

标签: java android arraylist

使用以下函数我可以创建ArrayList并将任何值附加到其中。这个动作不是问题,但在每次我无法追加到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;
}

2 个答案:

答案 0 :(得分:1)

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();
    ...

答案 1 :(得分:0)

我认为这会告诉你为什么它不起作用

public List<String> addItem(String temp) {
     ArrayList<String> listS = new ArrayList<>();
     listS.add(temp);
     return listS;
}

以及它将如何运作

ArrayList<String> listS = new ArrayList<>();
public List<String> addItem(String temp) {         
     listS.add(temp);
     return listS;
}

很抱歉快速编码,但问题的答案只是:)