john smith
john smith

Reputation: 185

How can I access String members of an arrayList to concatenate to them?

I have an arrayList of strings in my JDBC program, and I want to access them in order to add the contents of different columns to individual strings, but the concat method is not working for my strings, for whatever reason.

while ( project_set.next()){
    for (int i=3;i<=meta.getColumnCount();i++){
        int arr_pos = i-3;
        String temp =project_set.getString(i) + "\n";
        project_data.get(arr_pos).concat(temp);
    }
}

I simplified it a lot for the purposes of this question, but temp stores the data from a column, and then I am trying to concatenate that to a spot in the arrayList, which has already been filled by an empty string.

Upvotes: 0

Views: 101

Answers (2)

Reimeus
Reimeus

Reputation: 159874

String#concat returns a new String rather than modifying the original one as Strings are immutable. Instead set the List element to the result of concat:

project_data.set(arr_pos, project_data.get(arr_pos).concat(temp));

Aside: Use camelCase for variable names as defined in the Java naming conventions - e.g. projectData

Upvotes: 3

David
David

Reputation: 105

Strings are immutable, calling concat returns a new String. You would need to set temp to be the concatentation of your data from the column and the current value in the arrayList, then call ArrayList.set(index, temp) to store the new value.

Upvotes: 0

Related Questions