
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 9112 Articles for Object Oriented Programming

12K+ Views
Often, you will want to cycle through the elements in a collection. For example, you might want to display each element.The easiest way to do this is to employ an iterator, which is an object that implements either the Iterator or the ListIterator interface.Iterator enables you to cycle through a collection, obtaining or removing elements. ListIterator extends Iterator to allow bidirectional traversal of a list and the modification of elements.Before you can access a collection through an iterator, you must obtain one. Each of the collection classes provides an iterator() method that returns an iterator to the start of the ... Read More

15K+ Views
The indexOf(Object) method of the java.util.ArrayList class returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. Using this method, you can find the index of a given element.Example Live Demoimport java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList arrlist = new ArrayList(5); arrlist.add("G"); arrlist.add("E"); arrlist.add("F"); arrlist.add("M"); System.out.println("Size of list: " + arrlist.size()); for (String value : arrlist) { ... Read More

9K+ Views
To convert a comma separated String into an ArrayListSplit the String into an array of Strings using the split() method.Now, convert the obtained String array to list using the asList() method of the Arrays class.Example Live Demoimport java.util.Arrays; import java.util.List; public class Sample { public static void main(String[] args) { String myString = "JavaFx,Java,WebGL,OpenCV"; String[] myArray = myString.split(","); System.out.println("Contents of the array ::"+Arrays.toString(myArray)); List myList = Arrays.asList(myArray); } }OutputContents of the array ::[JavaFx, Java, WebGL, OpenCV]

9K+ Views
You can replace an element of an ArrayList using the set() method of the Collections class. This method accepts two parameters an integer parameter indicating the index of the element to be replaced and an element to replace with.Example Live Demoimport java.util.ArrayList; public class ArrayListSample { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("JavaFx"); list.add("Java"); list.add("WebGL"); list.add("OpenCV"); System.out.println(list); list.set(2, "HBase"); System.out.println(list); } }Output[JavaFx, Java, WebGL, OpenCV] [JavaFx, Java, HBase, OpenCV]

7K+ Views
To sort the contents of an ArrayList in descending orderCreate an ArrayList.Sort the contents of the ArrayList using the sort() method of the Collections class.Then, reverse array list using the reverse() method of the Collections class.Example:import java.util.ArrayList; import java.util.Collections; public class ArrayListSample { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("JavaFx"); list.add("Java"); list.add("WebGL"); list.add("OpenCV"); Collections.sort(list); System.out.println(list); Collections.reverse(list); System.out.println(list); } }Output:[Java, JavaFx, OpenCV, WebGL] [WebGL, OpenCV, JavaFx, Java]

670 Views
You can sort an ArrayList using the sort() method of the Collections class this method accepts a list object as a parameter and sorts the contents of it in ascending order.Example:import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; public class ArrayListSample { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("JavaFx"); list.add("Java"); list.add("WebGL"); list.add("OpenCV"); Set set = new LinkedHashSet(list); Collections.sort(list); System.out.println(list); } }Output:[Java, JavaFx, OpenCV, WebGL]

256 Views
The synchronizedList(List list) method of the Collections class accepts a List object and returns a synchronized list backed by the specified list.Example:import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; public class ArrayListSample { public static void main(String[] args){ ArrayList list = new ArrayList(); list.add("JavaFx"); list.add("Java"); list.add("WebGL"); list.add("OpenCV"); Set set = new LinkedHashSet(list); Collections.synchronizedList(list); System.out.println(list); } }Output:[JavaFx, Java, WebGL, OpenCV]

436 Views
The Collections class provides a method named reverse() this method accepts a list and reverses the order of elements in it.Example:import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; public class Sample { public static void main(String[] args){ ArrayList list = new ArrayList(); list.add("JavaFx"); list.add("Java"); list.add("WebGL"); list.add("OpenCV"); Set set = new LinkedHashSet(list); System.out.println(set); Collections.reverse(list); System.out.println(list); } }Output:[JavaFx, Java, WebGL, OpenCV] [OpenCV, WebGL, Java, JavaFx]

87 Views
The removeLast() method of the java.util.LinkedList class removes and returns the last element of this 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); System.out.println("Last element:" + list.removeLast()); System.out.println("LinkedList:" + list); } }Output:LinkedList:[Hello, 2, Chocolate, 10] Last element:10 LinkedList:[Hello, 2, Chocolate]

132 Views
The removeFirst() method of the java.util.LinkedList class removes and returns the first element from this 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); System.out.println("First element:" + list.removeFirst()); System.out.println("LinkedList:" + list); } }Output:LinkedList:[Hello, 2, Chocolate, 10] First element:Hello LinkedList:[2, Chocolate, 10]