2

Friends I have created an integer array qlist[] and it has some elements. Now i have inserted this integer array to data base in form of String as

Arrays.toString(qlist)

Now when i get it back again from db in future, I want to convert that string into Integer array .. Eg.

int[] qlist={13,4,12};

in database it looks like

[13,4,12]

as a string.

1

1 Answer 1

4

First of all save your String in a string variable.you need to remove [] brackets and then split your string by comma(,).

then you will have an string array containing only 1,2,3 values.you can easly convert them to integer by its position. here i have given the code for it.hope it will help.

String arr = "[1,2]";
 String[] items = arr.replaceAll("\\[", "").replaceAll("\\]", "").split(",");

 int[] results = new int[items.length];

for (int i = 0; i < items.length; i++) {
    try {
       results[i] = Integer.parseInt(items[i]);
    } catch (NumberFormatException nfe) {};
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.