Skip to content
Fix Code Error

Convert ArrayList to String[] array

March 13, 2021 by Code Error
Posted By: Anonymous

I’m working in the android environment and have tried the following code, but it doesn’t seem to be working.

String [] stockArr = (String[]) stock_list.toArray();

If I define as follows:

String [] stockArr = {"hello", "world"};

it works. Is there something that I’m missing?

Solution

Use like this.

List<String> stockList = new ArrayList<String>();
stockList.add("stock1");
stockList.add("stock2");

String[] stockArr = new String[stockList.size()];
stockArr = stockList.toArray(stockArr);

for(String s : stockArr)
    System.out.println(s);
Answered By: Anonymous

Related Articles

  • ListView inside ScrollView is not scrolling on Android
  • Android Layout Animations from bottom to top and top…
  • Android : Fragment doesn't fit into Fragment Container
  • How to implement HorizontalScrollView like Gallery?
  • How to set dropdown arrow in spinner?
  • Vue js vuecli3 application does not work in ie11…
  • Constraint Layout Vertical Align Center
  • How to convert number to words in java
  • Error inflating class android.support.v7.widget.Toolbar?
  • android.view.InflateException: Binary XML file line…
  • Fragment Not Showing in the Activity
  • Android ListView headers
  • Getting duplicated cardviews using SwipeRefresh Layout
  • Failed to load AppCompat ActionBar with unknown…
  • Android adding simple animations while…
  • MaterialCardView is in front of NavigationView. How…
  • onClick for buttons on custom CardView
  • How to use ScrollView in Android?
  • Remove Text from the edit text when edit text is focused
  • Percentage width in a RelativeLayout
  • How to add Typescript to a Nativescript-Vue project?
  • java.lang.NullPointerException: Attempt to invoke…
  • Unable to run Robolectric and Espresso with a…
  • How to center the content inside a linear layout?
  • GridLayout and Row/Column Span Woe
  • Button is null object reference (Android Studio)
  • Error inflating class…
  • AppCompat v7 r21 returning error in values.xml?
  • How to format a phone number in a textfield
  • Passing ArrayList through Intent
  • Android App crashes on animated gradient background
  • Integrate ZXing in Android Studio
  • Programmatically Hide/Show Android Soft Keyboard
  • Unable to move from one fragment to another
  • Maven2: Missing artifact but jars are in place
  • Android: How to Programmatically set the size of a Layout
  • Couldn't load memtrack module Logcat Error
  • Text size and different android screen sizes
  • Google in-app billing, a toast breaks everything
  • How to Intialize the Binding Properties in the…
  • Navigation View and Constraint Layout
  • Android app unable to start activity componentinfo
  • Fit Image in ImageButton in Android
  • Failed linking file resources
  • Android - styling seek bar
  • How to hide toolbar in collapsed mode and show in…
  • java.lang.RuntimeException: Unable to start activity…
  • Auto-fit TextView for Android
  • how to show progress bar(circle) in an activity…
  • Gridview with two columns and auto resized images
  • NullPointerException: Attempt to invoke virtual…
  • Android Layout Right Align
  • How to get the design view to look the same on your device
  • For loops print only last value
  • CardView not showing Shadow in Android L
  • Android TextView Text not getting wrapped
  • READ_EXTERNAL_STORAGE permission for Android
  • ADB - Android - Getting the name of the current activity
  • Moving from one activity to another Activity in Android
  • Move to another EditText when Soft Keyboard Next is…
  • Fragment not showing properly
  • How to make custom dialog with rounded corners in android
  • Android + Pair devices via bluetooth programmatically
  • Add Items to ListView - Android
  • How to disable keypad popup when on edittext?
  • DatePicker onDateSet method not running
  • GridLayout (not GridView) how to stretch all children evenly
  • Defining a percentage width for a LinearLayout?
  • Playing HTML5 video on fullscreen in android webview
  • This app won't run unless you update Google Play…
  • Android:java.lang.OutOfMemoryError: Failed to…
  • How to select and view database with specific value?
  • Google Maps Android API v2 - Interactive InfoWindow…
  • Put buttons at bottom of screen with LinearLayout?
  • Android Transparent TextView?
  • Adding custom radio buttons in android
  • How do I align views at the bottom of the screen?
  • Duplicate ID, tag null, or parent id with another…
  • Only last child from Firebase is displayed in recyclerview
  • Android: how to draw a border to a LinearLayout
  • How do I make a LinearLayout scrollable?
  • How to create a Custom Dialog box in android?
  • hardcoded string "row three", should use @string resource
  • Android "elevation" not showing a shadow
  • Efficiency of Java "Double Brace Initialization"?
  • How to get current location in Android
  • How to center the elements in ConstraintLayout
  • Android "gps requires ACCESS_FINE_LOCATION" error,…
  • android.content.res.Resources$NotFoundException:…
  • Android list view inside a scroll view
  • Google Maps Android API v2 Authorization failure
  • Create a custom View by inflating a layout?
  • how to access downloads folder in android?
  • How does PHP 'foreach' actually work?
  • Difference between a View's Padding and Margin
  • Android - How to download a file from a webserver
  • Android - java.lang.SecurityException: Permission…
  • Unable to get Notification pop-up
  • late event seems not being dropped when doing…
  • How to create Custom Ratings bar in Android

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How do I format XML in Notepad++?

Next Post:

jQuery checkbox change and click event

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error