简体   繁体   中英

Remove Null from For loop of ArrayList

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 )

Just check if "o" is null.

if (o == null)

You should initialize questionString to "" before the loop;

String questionString = "";

You're using SQL (SQLite specifically). That means you should avoid storing an ArrayList as a String.

Instead, you want to look into database normalization and the relational model. Rather than having a single row with a list in it, you should use multiple rows for each list element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM