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

1
  • 2
    Use the List#get(int) method. Commented Jan 2, 2014 at 4:58

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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);
@user3040563 No that date(string) is deprecated, look here :stackoverflow.com/questions/6876155/…
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.

2 Comments

You can do it in one step with stock_list.toArray(new String[stock_list.size()]);
Not intended as an optimization, just an observation. If you want to talk about readability, why not suggest stockList?
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());
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.