Reputation: 491
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 by the following code:
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]);
}
}
}
Upvotes: 49
Views: 347555
Reputation: 21
A three line solution, but works quite well:
int[] source_array = {0,1,2,3,4,5,6,7,8,9,10,11};
ArrayList<Integer> target_list = new ArrayList<Integer>();
for(int i = 0; i < source_array.length; i++){
target_list.add(random_array[i]);
}
Upvotes: 2
Reputation: 3725
object get(int index) is used to return the object stored at the specified index within the invoking collection.
import java.util.*;
class main
{
public static void main(String [] args)
{
ArrayList<String> arr = new ArrayList<String>();
arr.add("Hello!");
arr.add("Ishe");
arr.add("Watson?");
System.out.printf("%s\n",arr.get(2));
for (String s : arr)
{
System.out.printf("%s\n",s);
}
}
}
Upvotes: 1
Reputation: 2164
First of all you will need to define, which data type you need to keep in your list. As you have mentioned that the data is going to be String, the list should be made of type String.
Then if you want to get all the elements of the list, you have to just iterate over the list using a simple for loop or a for each loop.
List <String> list = new ArrayList<>();
list.add("A");
list.add("B");
for(String s : list){
System.out.println(s);
}
Also, if you want to use a raw ArrayList instead of a generic one, you will have to downcast the value. When using the raw ArrayList, all the elements are stored in form of Object.
List list = new ArrayList();
list.add("A");
list.add("B");
for(Object obj : list){
String s = (String) obj; //downcasting Object to String
System.out.println(s);
}
Upvotes: 0
Reputation: 2578
Java 8 introduced default implementation of forEach() inside the Iterable interface , you can easily do it by declarative approach .
List<String> values = Arrays.asList("Yasir","Shabbir","Choudhary");
values.forEach( value -> System.out.println(value));
Here is the code of Iterable interface
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
Upvotes: 0
Reputation: 22775
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.
}
Upvotes: 6
Reputation: 383676
The following snippet gives an example that shows how to get an element from a List
at a specified index, and also how to use the advanced for-each loop to iterate through all elements:
import java.util.*;
//...
List<String> list = new ArrayList<String>();
list.add("Hello!");
list.add("How are you?");
System.out.println(list.get(0)); // prints "Hello!"
for (String s : list) {
System.out.println(s);
} // prints "Hello!", "How are you?"
Note the following:
List<String>
and ArrayList<String>
types are used instead of raw ArrayList
type.list
is declared as List<String>
, i.e. the interface type instead of implementation type ArrayList<String>
.class ArrayList<E> implements List<E>
interface List<E>
E get(int index)
The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.
Effective Java 2nd Edition: Item 23: Don't use raw types in new code
If you use raw types, you lose all the safety and expressiveness benefits of generics.
[...] you should favor the use of interfaces rather than classes to refer to objects. If appropriate interface types exist, then parameters, return values, variables, and fields should all be declared using interface types.
Variables: Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter.
Upvotes: 53
Reputation: 10151
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);
}
Upvotes: 1
Reputation: 23950
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);
}
Upvotes: 1
Reputation: 36096
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:
ArrayList<String> list = new ArrayList<String>();
Upvotes: 3
Reputation: 2346
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));
}
Upvotes: 2