How to replace ArrayList<String> with String[] array

Asked 2 years ago, Updated 2 years ago, 92 views

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

This works

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

Why isn't this working? How do I change ArrayList to String[]?

java arraylist

2022-09-22 16:20

1 Answers

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


2022-09-22 16:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.