8

I receive some data as a JSON response from a server. I extract the data I need and I want to put this data into a string array. I do not know the size of the data, so I cannot declare the array as static. I declare a dynamic string array:

String[] xCoords = {};

After this I insert the data in the array:

   for (int i=0; i<jArray.length(); i++) {
         JSONObject json_data = jArray.getJSONObject(i);
         xCoords[i] = json_data.getString("xCoord");
   }

But I receive the

java.lang.ArrayIndexOutOfBoundsException
Caused by: java.lang.ArrayIndexOutOfBoundsException

What is the way to dynamically insert strings into a string array?

3 Answers 3

40

Use ArrayList although it is not really needed but just learn it:

ArrayList<String> stringArrayList = new ArrayList<String>();

   for (int i=0; i<jArray.length(); i++) {
         JSONObject json_data = jArray.getJSONObject(i);
         stringArrayList.add(json_data.getString("xCoord")); //add to arraylist
   }

//if you want your array
String [] stringArray = stringArrayList.toArray(new String[stringArrayList.size()]);
Sign up to request clarification or add additional context in comments.

8 Comments

I tried with this also, but if I type only String [] xCoords = xCoordsList.toArray(); it gives me a Type mismatch: cannot convert from Object[] to String[], so I make the line to String [] xCoords = (String[]) xCoordsList.toArray(); but this afterwards shows an 08-04 11:54:24.462: ERROR/AndroidRuntime(1173): Caused by: java.lang.ClassCastException: [Ljava.lang.Object; error.
sorry I edited the answer .. just add String cast to the last statement(String[])
This was my point - even with the edited version the application stops (you have to Force close it) and the LogCat shows a java.lang.ClassCastException: [Ljava.lang.Object; Caused by: java.lang.ClassCastException: [Ljava.lang.Object;
Are you sure you are not using ngArrayList.add(json_data);?? You must use ngArrayList.add(json_data.getString("xCoord")); I do not think you are using my code
Yes, sure about that - here is my code: ArrayList<String> xCoordsArrayList = new ArrayList<String>(); String[] xCoords = {}; JSONArray jArray = new JSONArray(result); for (int i=0; i<jArray.length(); i++) { JSONObject json_data = jArray.getJSONObject(i); xCoordsArrayList.add(json_data.getString("xCoord")); } xCoords = (String[]) xCoordsArrayList.toArray();
|
6

Try like this

 String stringArray[];        
 stringArray=new String[jArray.length()];
 String xCoords[]=new String[jArray.length()];;

       for (int i=0; i<jArray.length(); i++) {
             JSONObject json_data = jArray.getJSONObject(i);
             xCoords[i] = json_data.getString("xCoord");
       }

4 Comments

This works when inserting but when I try to extract the data from it with for (int i=0; i<xCoords.length; i++){ double xCoord = Double.parseDouble(xCoords[i]); } I receive the 08-04 11:40:30.435: ERROR/AndroidRuntime(1098): java.lang.NullPointerException. How can I extract the data?
check value at xCoords[i].Print a log to see the value.Is there a problem with length or just it is because xCoords[i] is not double value?
The problem is with the length - if I change the parameters of the for {} to for (int i=0; i<10; i++) the error moves to the next line - double xCoord = Double.parseDouble(xCoords[i]);
then you have problem in length and also in xCooords[i],it is not always double
0

String Array in Java has a defined size that should be given while declaration, you cannot change it later by adding or removing elements and the pure concept of the dynamic array does not exit in java. Read detailed article here...

This is the right way to declare an array of fixed size.

        String[] myString = new String[5];
        fruits[0]="hello";
        fruits[1]="hello";
        fruits[2]="hello";
        fruits[3]="hello";
        fruits[4]="hello";

Instead, you can use a List to perform a similar task. The list is purely dynamic and you can add multiple values on runtime. This is the right way to declare a list in Android using JAVA.

   ArrayList<String> fruits = new ArrayList<String>();
        fruits.add("Value 1");
        fruits.add("Value 2");
        fruits.add("Value 3");
        fruits.add("Value 4");
        fruits.add("Value 5");
        fruits.add("Value 6");
        fruits.add("Value 7");
// add as much values as per requirement
// you can also use loops to add multiple values

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.