- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 34483 Articles for Programming
![V Jyothi](https://www.tutorialspoint.com/assets/profiles/13530/profile/60_77592-1512648869.jpg)
134 Views
The addFirst(E e) method of the class java.util.LinkedList inserts the specified element at the beginning of this list.Example:public class LinkedListDemo { public static void main(String[] args) { LinkedList list = new LinkedList(); list.add("Hello"); list.add(2); list.add("Chocolate"); list.add("10"); System.out.println("LinkedList:" + list); list.addFirst("Element"); System.out.println("LinkedList:" + list); } }Output:LinkedList:[Hello, 2, Chocolate, 10] LinkedList:[Element, Hello, 2, Chocolate, 10]
![Priya Pallavi](https://www.tutorialspoint.com/assets/profiles/13562/profile/60_91320-1512544134.jpg)
246 Views
The add(int index, E element) method of the java.util.ArrayList class inserts the specified element E at the specified position in this list. It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices).Exampleimport java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList arrlist = new ArrayList(5); arrlist.add(15); arrlist.add(22); arrlist.add(30); arrlist.add(40); arrlist.add(2,25); for (Integer number : arrlist) { System.out.println("Number = " + number); } } }OutputNumber = 15 Number = 22 Number = 25 Number = 30 Number = 40
![Nikitha N](https://www.tutorialspoint.com/assets/profiles/13558/profile/60_76068-1512713229.jpg)
281 Views
The add(E e) method of the java.util.ArrayList class appends the specified element E to the end of the list.Example:import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList arrlist = new ArrayList(5); arrlist.add(15); arrlist.add(20); arrlist.add(25); for (Integer number : arrlist) { System.out.println("Number = " + number); } } }Output:Number = 15 Number = 20 Number = 25
![Srinivas Gorla](https://www.tutorialspoint.com/assets/profiles/13556/profile/60_78057-1512712331.jpg)
75 Views
The removeRange() method of the ArrayList class removes all of the elements from this List whose index is between fromIndex and toIndex.Exampleimport java.util.*; public class ArrayListDemo extends ArrayList{ public static void main(String[] args) { ArrayListDemo arrlist = new ArrayListDemo(); arrlist.add(10); arrlist.add(12); arrlist.add(31); System.out.println("The list:" + arrlist); arrlist.removeRange(0,2); System.out.println("The list after using removeRange:" + arrlist); } }OutputThe list:[10, 12, 31] The list after using removeRange:[31]
![Abhinanda Shri](https://www.tutorialspoint.com/assets/profiles/13552/profile/60_73264-1512711555.jpg)
89 Views
The set() method of the ArrayList class replaces the element at the specified position in this list with the specified element.Exampleimport java.util.ArrayList; public class Sample { public static void main(String args[]) { ArrayList al = new ArrayList(); System.out.println("Initial size of al: " + al.size()); al.add("C"); al.add("A"); al.add("E"); al.add(1, "A2"); System.out.println("Size of al after additions: " + al.size()); System.out.println("Contents of al: " + al); System.out.println("Size of al after deletions: " + al.size()); ... Read More
![Ankitha Reddy](https://www.tutorialspoint.com/assets/profiles/13550/profile/60_73963-1512711511.jpg)
54 Views
The listIterator(int index) method of the java.util.LinkedList class returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list. Example import java.util.*; public class LinkedListDemo { public static void main(String[] args) { LinkedList list = new LinkedList(); list.add("Hello"); list.add(2); list.add("Chocolate"); list.add("10"); System.out.println("LinkedList:" + list); Iterator x = list.listIterator(1); while (x.hasNext()) { System.out.println(x.next()); } } } Output LinkedList:[Hello, 2, Chocolate, 10] 2 Chocolate 10
![Abhinaya](https://www.tutorialspoint.com/assets/profiles/13548/profile/60_54851-1512705920.jpg)
104 Views
The trimToSize() method of the java.util.ArrayList class trims the capacity of this ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance.Exampleimport java.util.ArrayList; public class ArrayListDemo { public static void main(String args[]) { ArrayList arrlist = new ArrayList(5); arrlist.add(35); arrlist.add(20); arrlist.add(25); arrlist.trimToSize(); for (Integer number : arrlist) { System.out.println("Number = " + number); } } }OutputNumber = 35 Number = 20 Number = 25
![Ramu Prasad](https://www.tutorialspoint.com/assets/profiles/13539/profile/60_87996-1512650215.jpg)
769 Views
Since a vector stores elements in the form of objects, you can store objects of various types (heterogeneous) in it.Example:import java.util.*; class Demo{} public class VectorSample { public static void main(String args[]) { Demo obj = new Demo(); Vector v = new Vector(3, 2); System.out.println("Initial size: " + v.size()); System.out.println("Initial capacity: " + v.capacity()); v.addElement(new Integer(1)); v.addElement(new String("krishna")); v.addElement(new Float(3.5f)); v.addElement(obj); System.out.println("Capacity after four additions: " + v.capacity()); } }
![Sravani S](https://www.tutorialspoint.com/assets/profiles/13538/profile/60_67753-1512711355.jpg)
2K+ Views
The size of a vector represents the number of components in the vector. The capacity of a vector represents the maximum number of elements the vector can hold.Example:import java.util.*; public class VectorDemo { public static void main(String args[]) { Vector v = new Vector(3, 2); System.out.println("Initial size: " + v.size()); System.out.println("Initial capacity: " + v.capacity()); v.addElement(new Integer(1)); v.addElement(new Integer(2)); v.addElement(new Integer(3)); v.addElement(new Integer(4)); System.out.println("Capacity after four additions: " + v.capacity()); v.addElement(new ... Read More
![Janani Jaganathan](https://www.tutorialspoint.com/assets/profiles/506990/profile/60_3923617-1661405985.jpg)
507 Views
Both Vector and ArrayList implement the List interface, and each of them uses (dynamically resizable) arrays for their internal data structure, similar to using an ordinary array. However, there are many differences between ArrayList and Vector classes hence by reading this article, you will learn what ArrayList and Vector class are and their major difference that helps you to choose which one to opt for. Understanding ArrayList and Vector Class In addition to the Arrays class, Java provides an ArrayList class that can be used to create containers that stores lists of objects. ArrayList is considered to be a growable ... Read More