Asked  7 Months ago    Answers:  5   Viewed   34 times

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?

 Answers

13

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);
Tuesday, June 1, 2021
 
maniclorn
answered 7 Months ago
20

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

Wednesday, July 28, 2021
 
Laimoncijus
answered 5 Months ago
16

JList provides a constructor JList(Object[]) which you can call after unpacking your ArrayList<String> using toArray():

list = new JList(arr.toArray())
Thursday, July 29, 2021
 
Manmay
answered 5 Months ago
46

to get the desired output:

replace these lines

String[] arr = null;
while (rs.next()) {
    String em = rs.getString("EM_ID");
    arr = em.split("n");
    for (int i =0; i < arr.length; i++){
        System.out.println(arr[i]);
    }
}

by

String arr = null;
while (rs.next()) {
    String em = rs.getString("EM_ID");
    arr = em.replace("n", ",");
    System.out.println(arr);
}
Saturday, August 14, 2021
 
Bálint Molnár
answered 4 Months ago
24

Use a second ArrayList for the 3 strings, not a primitive array. Ie.
private List<List<String>> addresses = new ArrayList<List<String>>();

Then you can have:

ArrayList<String> singleAddress = new ArrayList<String>();
singleAddress.add("17 Fake Street");
singleAddress.add("Phoney town");
singleAddress.add("Makebelieveland");

addresses.add(singleAddress);

(I think some strange things can happen with type erasure here, but I don't think it should matter here)

If you're dead set on using a primitive array, only a minor change is required to get your example to work. As explained in other answers, the size of the array can not be included in the declaration. So changing:

private ArrayList<String[]> addresses = new ArrayList<String[3]>();

to

private ArrayList<String[]> addresses = new ArrayList<String[]>();

will work.

Saturday, September 4, 2021
 
Blur
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share