The function returns an empty list after .clear ()

I am working on a Badge project with NFC. So I need to display all the values ​​from the SQL query. I am currently trying to sort the values ​​by day and insert them into a new list that I want to add to another list that should contain all days.

public ArrayList<ArrayList<String>> sort(List<String> daten) throws SQLException, ParseException {
    int i = 0;
    int position = 0;
    long firstday= parse(daten.get(0)).getTime();
    int arraysize = daten.size();
    ArrayList<ArrayList<String>> alldays = new ArrayList<ArrayList<String>>();
    ArrayList<String> day = new ArrayList<String>();
    while (i <= arraygrösse) {
        if (parse(daten.get(position)).getTime() - firstday< 72000000) {
            day.add(daten.get(position));
            daten.remove(position);
        } else {
            alldays.add(day);
            firstday= parse(daten.get(0)).getTime();
            day.clear();
        }
        i++;
    }
    return alldays;
}

      

After calling the function .clear () "alldays" is empty ([ [][][]

] Is there any other option than .clear (), I also tried .removeAll (), but it didn't work either.

+3


source to share


3 answers


instead of day.clear () try this:



day = new ArrayList<String>();

      

+6


source


When you are add(day)

, you do not add up, you add the same day

number of times, and therefore the subsequent operations add

and clear

will affect it.

I would expect day

and alldays

would be defined in this method.



public ArrayList<ArrayList<String>> sort(List<String> daten) throws SQLException, ParseException {
    int i = 0;
    int position = 0;

    //new local delarations
    ArrayList<ArrayList<String>> alldays = new ArrayList<ArrayList<String>>();
    ArrayList<String> day = new ArrayList<String>();

    long firstday= parse(daten.get(0)).getTime();
    int arraysize = daten.size();
    while (i <= arraygrösse) {
        if (parse(daten.get(position)).getTime() - firstday< 72000000) {
            day.add(daten.get(position));
            daten.remove(position);
        } else {
            alldays.add(day);
            firstday= parse(daten.get(0)).getTime();

            //don't clear the day, start a new day
            day = new ArrayList<String>();
        }
        i++;
    }
    return alldays;
}

      

+3


source


You always add day

to alldays

. Therefore, if you cleanse day

, it must also cleanse alldays

.

You need to allocate a new array instead of .clear()

;

day = new ArrayList<String>();

      

+1


source







All Articles