Remove Null from For loop of ArrayList

Tags: , , , ,



I am currently converting an ArrayList to a String to send it to a DB so I can retrieve it on the other end and convert it back to an ArrayList later.

My thought process is to convert it to a string currently by doing the following

for(Integer o: questions) questionString += o + ",";

questions is the ArrayList and looks like this: [4, 4]

When I look at my database it looks like this null4,4,

How would I go about removing null at the start and the last , so it looks like 4,4 on the database?

On the other end I can then get it by using the following:

ArrayList myList = new ArrayList<String>(Arrays.asList(arrayQuestions.split(",")));

( I am using SQLite which is a requirement for my college project )

Answer

You should initialize questionString to “” before the loop;

String questionString = "";


Source: stackoverflow