Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

how to retrieve a element from an ArrayList in Collections

how to retrieve a element from an ArrayList in Collections

Previous Home Next

 

In this example we will see how can we retrieve an element from an ArrayList.

In this example we have created a class CollectionExample. For using ArrayList we have
to import the java.util package.
 In the following example we have used the get(int index) method on the ArrayList for retrieving the element stored at the specified index.

In this example we have created an ArrayList and then added a few element to it by using the add(Object obj) method on the ArrayList object. For getting the desired element we have used the get(int index) method on the ArrayList object. The following statement in the given example retrieves the element at the desired index.

al.get(2);

 
package r4r.co.in;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Collection;



public class CollectionExample {

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub

ArrayList<String> al=new ArrayList<String>(); // creating an ArrayList named al
al.add("r4r"); // adding string objects to the arraylist
al.add("development");
al.add("tutorials");
al.add("shashi");
al.add("s/w engineer");
System.out.println("The size of the arraylist is: "+al.size()); // determining the length of the arraylist
System.out.println("The contents of the list are :"+al); // showing the contents of the arraylist
System.out.println(al.get(2)); // retrieves the element stored at index 2
System.out.println(al.contains("r4r")); // finds whether object r4r is in the arraylist or not, returns a boolean value ie, true or false
System.out.println(al.indexOf("shashi")); // displays the index of shashi
System.out.println(al.isEmpty()); // checks whether the al is empty or not, returns a bool value
}


}

The output of the above written code is as follows:

The size of the arraylist is: 5
The contents of the list are :[r4r, development, tutorials, shashi, s/w engineer]
tutorials
true
3
false

Previous Home Next