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]); } } }
So, first, make ArrayList as an instance variable to the PetList class so that it can be accessible through an object even outside the constructor. Then, you can provide an eatAll() method which iterates the ArrayList<Pet> and call the eat() method on all pet objects.
The get() method of ArrayList in Java is used to get the element of a specified index within the list. Parameter: Index of the elements to be returned. It is of data-type int. Return Type: The element at the specified index in the given list.
ArrayList in Java is used to store dynamically sized collection of elements. Contrary to Arrays that are fixed in size, an ArrayList grows its size automatically when new elements are added to it. ArrayList is part of Java's collection framework and implements Java's List interface.
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)
JLS 4.8 Raw Types
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.
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. }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With