Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » Java programs » Java list programs

Get an element/object from given index of a List in Java



Here, we are going to learn how to get an element/object from given index of a list in java?
Submitted by IncludeHelp, on October 10, 2018

Given a list of integers and we have to access/retrieve element/object from given index from the list in Java.

To get the element from given index - we use List.get() method, it is a library method of the List, which returns object/element from given index.

Syntax:

    List.get(index);

Program:

import java.util.*;
public class ListExample {
	public static void main (String[] args) 
	{
		// creating a list of integers
		List<Integer> int_list = new ArrayList<Integer> ();

		//adding some of the elements
		int_list.add (10);
		int_list.add (20);
		int_list.add (30);
		int_list.add (40);
		int_list.add (50);

		//printing the empty list 
		System.out.println ("Element at " + 0 + " index = " + int_list.get (0)) ;
		System.out.println ("Element at " + 1 + " index = " + int_list.get (1)) ;
		System.out.println ("Element at " + 2 + " index = " + int_list.get (2)) ;
		System.out.println ("Element at " + 3 + " index = " + int_list.get (3)) ;
		System.out.println ("Element at " + 4 + " index = " + int_list.get (4)) ;
	}
};

Output

Element at 0 index = 10
Element at 1 index = 20
Element at 2 index = 30
Element at 3 index = 40
Element at 4 index = 50





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.