0
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"?

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
Santino 'Sonny' Corleone
  • 1,735
  • 5
  • 25
  • 52

3 Answers3

5

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

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Suppose I have a date and time seperately.and I want to combine both of them to date.How to do it?Something like this `Time dateString=x14; Time dateString2=x16; Date dateString1=x15; Date d1 = new Date(dateString1+" "+dateString);` – Santino 'Sonny' Corleone Jan 02 '14 at 05:10
  • @user3040563 No that date(string) is deprecated, look here :http://stackoverflow.com/questions/6876155/java-date-constructor-accepts-a-date-string-but-deprecated-tried-alternative – Suresh Atta Jan 02 '14 at 05:19
1

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.

stinepike
  • 54,068
  • 14
  • 92
  • 112
0
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());
   }
Shriram
  • 4,343
  • 8
  • 37
  • 64