By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,736 Members | 1,982 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,736 IT Pros & Developers. It's quick & easy.

How to read item by item from String ArrayList

P: 2
Some can help me to extract String by String from ArrayList.

I can add an String but now I would like to read item by item from an ArrayList.

Thanks
Jan 26 '07 #1
Share this Question
Share on Google+
3 Replies


P: 2
Please, some one can help me to extract String by String from ArrayList.

I can add an String but now I would like to read item by item from an ArrayList.

Thanks
Jan 26 '07 #2

Expert 100+
P: 1,510
Please, some one can help me to extract String by String from ArrayList.

I can add an String but now I would like to read item by item from an ArrayList.

Thanks
you can use an Iterator, e.g.
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class ArrayListString 
  5. {
  6. public static void main(String s[])
  7.  {
  8.    ArrayList <String>list = new ArrayList <String>(100);    // can store any Nunber
  9.    list.add("Hello");
  10.    list.add("world");
  11.    Iterator iter = list.iterator();
  12.     while (iter.hasNext()) {
  13.       System.out.println("string " + iter.next());
  14.       }
  15.   }
  16. }
when run give
string Hello
string world
Jan 26 '07 #3

10K+
P: 13,264
you can use an Iterator, e.g.
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class ArrayListString 
  5. {
  6. public static void main(String s[])
  7.  {
  8.    ArrayList <String>list = new ArrayList <String>(100);    // can store any Nunber
  9.    list.add("Hello");
  10.    list.add("world");
  11.    Iterator iter = list.iterator();
  12.     while (iter.hasNext()) {
  13.       System.out.println("string " + iter.next());
  14.       }
  15.   }
  16. }
when run give
string Hello
string world
Alternatively you could also read the strings out of the list using the get method of arraylist.
Jan 29 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.