Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an array list in Java?

Tags:

java

arraylist

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]);         }     } } 
like image 878
soad El-hayek Avatar asked Apr 23 '10 08:04

soad El-hayek


People also ask

How do you call a method using an ArrayList of objects in Java?

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.

How do I get an element from an ArrayList?

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.

What is the function of ArrayList in Java?

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.


2 Answers

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:

  • Generic List<String> and ArrayList<String> types are used instead of raw ArrayList type.
  • Variable names starts with lowercase
  • list is declared as List<String>, i.e. the interface type instead of implementation type ArrayList<String>.

References

API:

  • Java Collections Framework tutorial
  • class ArrayList<E> implements List<E>
  • interface List<E>
    • E get(int index)
      • Returns the element at the specified position in this list.

Don't use raw types

  • 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.

Prefer interfaces to implementation classes in type declarations

  • Effective Java 2nd Edition: Item 52: Refer to objects by their interfaces

    [...] 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.

Naming conventions

Variables: Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter.

like image 94
polygenelubricants Avatar answered Sep 21 '22 13:09

polygenelubricants


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. } 
like image 25
jjujuma Avatar answered Sep 20 '22 13:09

jjujuma