Reputation: 1753
ArrayList<String> stock_list = new ArrayList<String>();
stock_list.add("stock1");
stock_list.add("stock2");
I have this ArrayList, how do I separate into separate Strings so that I get String a="stock1"; String b="stock2"?
Upvotes: 0
Views: 84
Reputation: 4411
ArrayList<String> list = new ArrayList<String>();
list.add("xxx");
Object[] array = list.toArray();
for (int i = 0; i < array.length; i++) {
System.out.println(" value = " + array[i].toString());
}
Upvotes: 0
Reputation: 54672
just an alternative answer
String[] temp = new String[stock_list.size()];
stock_list.toArray(temp);
now temp[0]
contains the first string and temp[1]
contains the other.
Upvotes: 1
Reputation: 121998
Seperating an array into Strings
You are mistaken that it's an array. No, it is ArrayList
.
ArrayList implements List
and it have a method get()
which takes index as a arg. Just get those strings back with index.
Returns the element at the specified position in this list.
String a = stock_list.get(0);
String b = stock_list.get(1);
And you should consider to declare your ArrayList like
List<String> stock_list = new ArrayList<String>();
Which is called as Programming with Interfaces
Upvotes: 5