tags:

views:

170

answers:

6

I need to know if i store my data in an ArrayList and I need to get the value that I've stored in it

For example : if I have an array list like this

      ArrayList A = new ArrayList();
      A = {"Soad", "mahran"};

and I want to get each String element, how can I do it ?

I've tried to do it like this

package arraylist;

import java.util.ArrayList;

public class Main {

        public static void main(String[] args) {
        ArrayList S = new ArrayList();

        String A = "soad ";
        S.add(A);
        S.add("A");
        String F = S.toString();
        System.out.println(F);
        String [] W = F.split(",");
        for(int i=0 ; i<W.length ; i++) {
           System.out.println(W[i]);
        }
    }
}
+2  A: 

This should do the trick:

String elem = (String)S.get(0);

Will return the first item in array.

Or

for(int i=0 ; i<S.size() ; i++){
     System.out.println(S.get(i));
}
dvd
@dvd: Forget about what should do the trick, follow the correct coding practice and standards that @polygenelubricants described. You will thank him later for it.
Helen Neely
@Helen Neely: Yeah, i know about generics and use them more and more in my code. I just tried to provide simple answer for the question. When i saw other answers mentioned generics i decided to not duplicate them.
dvd
+4  A: 
polygenelubricants
+3  A: 

You could either get your strings by index (System.out.println(S.get(0));) or iterate through it:

for (String s : S) {
  System.out.println(s);
}

For other ways to iterate through a list (and their implications) see traditional for loop vs Iterator in Java.

Additionally:

  • you shouldn't use variable names starting with upper-case letters
  • you should parametrize your array list: ArrayList<String> list = new ArrayList<String>();
  • you should get familiar with Java's extensive API documentation (aka Javadoc), e.g. Java 5, Java 6
sfussenegger
+1  A: 

A List is an ordered Collection of elements. You can add them with the add method, and retrieve them with the get(int index) method. You can also iterate over a List, remove elements, etc. Here are some basic examples of using a List:

List<String> names = new ArrayList<String>(3); // 3 because we expect the list 
    // to have 3 entries.  If we didn't know how many entries we expected, we
    // could leave this empty or use a LinkedList instead
names.add("Alice");
names.add("Bob");
names.add("Charlie");
System.out.println(names.get(2)); // prints "Charlie"
System.out.println(names); // prints the whole list
for (String name: names) {
    System.out.println(name);  // prints the names in turn.
}
jjujuma
Defining the size of the list in the constructor is good practice for efficiency, but the list does auto-size so it isn't really required. It would be nice if you add that to the comment so beginners don't get confused.
extraneon
What is the '+ "\n"' for? println will handle new lines, and anyway you can't add a string to void (the return value of println).
DaveJohnston
@DaveJohnston thanks, I've fixed it - I think I need another coffee
jjujuma
+1  A: 

If you use Java 1.5 or beyond you could use:

List<String> S = new ArrayList<String>();
s.add("My text");

for (String item : S) {
  System.out.println(item);
}
extraneon
+1  A: 

You should read collections framework tutorial first of all.

But to answer your question this is how you should do it:

ArrayList<String> strings = new ArrayList<String>();
strings.add("String1");
strings.add("String2");

// To access a specific element:
System.out.println(strings.get(1));
// To loop through and print all of the elements:
for (String element : strings) {
    System.out.println(element);
}
DaveJohnston