在Android中将ArrayList转换为string数组

我有一个ArrayList,其中包含[ann,john]forms的值。 我想把这个ArrayList转换成{“ann”,“john”}forms的string数组。

我应该怎么做?

我的Android代码:

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

我的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 

我试着在答案中提到的一切,但我的logcat只是似乎并不正确

这是一个例外:

  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) 

这里还有一个:

  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) 

Solutions Collecting From Web of "在Android中将ArrayList转换为string数组"

使用方法“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]); } 

或者你可以这样做:(在其他答案中提到)

 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(&#xFF09;

一般来说:

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

或者更好的是,使用内置:

 List<String> names = new ArrayList<String>(); String[] namesArr = names.toArray(new String[names.size()]); 
 String[] array = new String[items2.size()]; items2.toArray(array); 
 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); 

我知道我很迟才回答这个问题。 但它会帮助别人,我也被这个困住了。

这是我做了什么工作。

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

您可以创build一个与ArrayList大小相同的ArrayList ,然后对迭代的循环进行索引,并将它们插入到数组中。