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

Integer List Example (Create, Add elements, Remove and print) in Java



Java list of integers example: Here, we are going to learn how to create a list of integers, how to add and remove the elements and how to print the list, individual elements?
Submitted by IncludeHelp, on October 11, 2018

What is list?

List in an interface, which is implemented by the ArrayList, LinkedList, Vector and Stack. It is an ordered collection of the object, where duplicate objects can also be stored.

In this example, we are performing following operations:

  • Creating a list of integers - implemented by ArrayList
  • Printing the empty List
  • Adding elements to the List and printing
  • Adding elements to the List at given indexes and printing
  • Removing elements from the List by index and printing

Following methods are using in the program:

  • List.add(element) - Adds element at the end of the List
  • List.add(index, element) - Inserts element at given index
  • List.remove(index) - Removes the element from given index

Example:

import java.util.*;

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

		//printing the list
		System.out.print ("list= ");
		System.out.println (list);

		//adding elements
		list.add (10);
		list.add (20);
		list.add (30);
		list.add (40);
		list.add (50);
		
		//printing the list 
		System.out.print ("list= ");
		System.out.println (list);

		//adding elements by specifying Iindex
		list.add (0, 5); //will add 5 at 0th Index
		list.add (3, 7); //will add 7 at 3th index 
		
		//printing the list 
		System.out.print ("list= ");
		System.out.println (list);

		//removeing elements
		list.remove (0); //wili remove element from index 0
		list.remove (3); //will remove element from index 3
		
		//printing the list 
		System.out.println ("list= ");
		System.out.println (list);
	}
};

Output

list= []
list= [10, 20, 30, 40, 50]
list= [5, 10, 20, 7, 30, 40, 50]
list=
[10, 20, 7, 40, 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.