[android] Convert ArrayList to String array in Android

I have a ArrayList that contains values in the form [ann,john]. I want to convert this ArrayList into a String array in the form {"ann","john"}.

How should I do this?

My Android code:

     final Button markabsent = (Button) findViewById(R.id.Button02);
      markabsent.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {

            // Perform action on click
             Toast.makeText(display.this,"You have marked the students absent",Toast.LENGTH_SHORT).show();

             SparseBooleanArray checkedabsent = lView.getCheckedItemPositions();



            for (int i = 0; i < arr2.length; i++) 
            {
                if (checkedabsent.get(i)) 
                {
                    items2.add(arr2[i]);
                    System.out.println(items2);

                }
            }

            Log.d("", "items:");
            for (String string : items2)
            {
                Log.d("string is", string);


            }  
          }
      });  

My Logcat:

     11-10 21:44:00.414: INFO/System.out(2316): [ann, john, ann]
     11-10 21:44:00.414: DEBUG/(2316): items:
     11-10 21:44:00.414: DEBUG/string is(2316): ann
     11-10 21:44:00.414: DEBUG/string is(2316): john

I tried doing everything mentioned in the answers but my logcat just doesn't seem to imrove

Here is the one giving exceptions:

          11-10 22:14:20.855: INFO/System.out(3322): [Ljava.lang.String;@44eb6ff0
          11-10 22:16:38.186: ERROR/AndroidRuntime(3411): FATAL EXCEPTION: main
          11-10 22:16:38.186: ERROR/AndroidRuntime(3411): java.lang.ClassCastException: [Ljava.lang.Object;
          11-10 22:16:38.186: ERROR/AndroidRuntime(3411):     at com.example.display$2.onClick(display.java:124)
          11-10 22:16:38.186: ERROR/AndroidRuntime(3411):     at android.view.View.performClick(View.java:2408)
          11-10 22:16:38.186: ERROR/AndroidRuntime(3411):     at android.view.View$PerformClick.run(View.java:8816)
          11-10 22:16:38.186: ERROR/AndroidRuntime(3411):     at android.os.Handler.handleCallback(Handler.java:587)

Here is one more:

          11-10 22:14:20.875: DEBUG/string is(3322): ann
          11-10 22:14:20.875: INFO/System.out(3322): [Ljava.lang.String;@44eb7720
          11-10 22:14:20.875: INFO/System.out(3322): [Ljava.lang.String;@44eb7720
          11-10 22:14:20.885: INFO/System.out(3322): [Ljava.lang.String;@44eb7720
          11-10 22:14:20.895: DEBUG/string is(3322): john
          11-10 22:14:20.895: INFO/System.out(3322): [Ljava.lang.String;@44eb7de8
          11-10 22:16:38.186: ERROR/AndroidRuntime(3411):     at com.example.display$2.onClick(display.java:124)

This question is related to android string arraylist

The answer is


Use the method "toArray()"

ArrayList<String>  mStringList= new ArrayList<String>();
mStringList.add("ann");
mStringList.add("john");
Object[] mStringArray = mStringList.toArray();

for(int i = 0; i < mStringArray.length ; i++){
    Log.d("string is",(String)mStringArray[i]);
}

or you can do it like this: (mentioned in other answers)

ArrayList<String>  mStringList= new ArrayList<String>();
mStringList.add("ann");
mStringList.add("john");
String[] mStringArray = new String[mStringList.size()];
mStringArray = mStringList.toArray(mStringArray);

for(int i = 0; i < mStringArray.length ; i++){
    Log.d("string is",(String)mStringArray[i]);
}

http://developer.android.com/reference/java/util/ArrayList.html#toArray()


You could make an array the same size as the ArrayList and then make an iterated for loop to index the items and insert them into the array.


I know im very late to answer this. But it will help others, I was also stucked on this.

Here is what i did to get it work.

String[] namesArr = (String[]) names.toArray(new String[names.size()]);

try this

List<String> list = new ArrayList<String>();
list.add("List1");
list.add("List2");

String[] listArr = new String[list.size()];
listArr = list.toArray(listArr );

for(String s : listArr )
    System.out.println(s);

Well in general:

List<String> names = new ArrayList<String>();
names.add("john");
names.add("ann");

String[] namesArr = new String[names.size()];
for (int i = 0; i < names.size(); i++) {
    namesArr[i] = names.get(i);  
}

Or better yet, using built in:

List<String> names = new ArrayList<String>();
String[] namesArr = names.toArray(new String[names.size()]);

String[] array = new String[items2.size()];
items2.toArray(array);

You can try this code

String[] stringA = new String[stringArrayList.size()];
stringArrayList.toArray(stringA)
System.out.println(stringA[0]);

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript

Examples related to arraylist

Adding null values to arraylist How to iterate through an ArrayList of Objects of ArrayList of Objects? Dynamically adding elements to ArrayList in Groovy How to replace existing value of ArrayList element in Java How to remove the last element added into the List? How to append elements at the end of ArrayList in Java? Removing Duplicate Values from ArrayList How to declare an ArrayList with values? In Java, can you modify a List while iterating through it? Load arrayList data into JTable