Dynamically inserting string into a string array in Android

Related searches

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

Caused by: java.lang.ArrayIndexOutOfBoundsException

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

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()]);

How to dynamically add elements to String array?, But, if you still want to do it then,Convert the array to ArrayList object. public class AddingItemsDynamically { public static void main(String� String array which contain multiple values can be easily converted into only single string value using Arrays.toString() method. This method is automatically converted string array into string variable. So here is the complete step by step tutorial for Convert String Array to single String value in Java Android.

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");

How to add items to an array in java dynamically?, Insert elements in array using arrayName.add() function on app program running time. Dynamically adding values in string array is very easy� Convert ArrayList to String array in Android. Ask Question the ArrayList and then make an iterated for loop to index the items and insert them into the array.

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];

Dynamically add values in string array list in android Java, How to create a COVID-19 Tracker Android App � Learn Java on Your Own Given an array of size n, the task is to add an element x in this array in Java. The size of the array cannot be changed dynamically in Java, as it is done in C/C++. insert the elements from public static void main(String[] args). If you had three parameters that you wanted to insert into the string, then they would be called 1$, 2$, and 3$. The order you place them in the resource string doesn't matter, only the order that you supply the parameters. format type: There are a lot of ways that you can format things (see the documentation). Here are some common ones: s string

How to add an element to an Array in Java?, Cases at comparable and do to declare dynamic string array and have a list. Series in Works for android: one of 20 elements to. Choices at Sortingwe can be null because java and i use an array search in that allows adding the last index. You can use either getString(int) or getText(int) to retrieve a string. getText(int) retains any rich text styling applied to the string. String array. An array of strings that can be referenced from the application.

Declare Dynamic String Array Java, Of course, if you know beforehand how many strings you are going to put in your array, you can create an array of that size and set the elements by using the correct position: String[] myarray = new String[numberofstrings]; myarray[23] = string24; //this sets the 24'th (first index is 0) element to string24.

String[] scripts = new String [] {"test3","test4","test5"}; Please read this tutorial on Arrays. Second, Arrays are fixed size, so you can't add new Strings to above array. You may override existing values. scripts[0] = string1; (or) Create array with size then keep on adding elements till it is full.

  • 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();
  • 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