- 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 10102 Articles for Object Oriented Programming

390 Views
The asList() method is used to convert an array to list.Example:import java.util.Arrays; import java.util.List; public class ArrayToList { public static void main(String args[]){ Integer[] myArray = {23, 93, 56, 92, 39}; List list = Arrays.asList(myArray); System.out.println(list); } }Output:[23, 93, 56, 92, 39]

32 Views
The lastIndexOf(Object) method of the class java.util.ArrayList returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.Exampleimport 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"); arrlist.add("E"); System.out.println("Size of list: " + arrlist.size()); for (String value : arrlist) { System.out.println("Value = " + value); ... Read More

110 Views
The List interface extends Collection and declares the behavior of a collection that stores a sequence of elements.Elements can be inserted or accessed by their position in the list, using a zero-based index.A list may contain duplicate elements.In addition to the methods defined by Collection, List defines some of its own, which are summarized in the following table.Several of the list methods will throw an UnsupportedOperationException if the collection cannot be modified, and a ClassCastException is generated when one object is incompatible with another.Examplepublic class CollectionsDemo { public static void main(String[] args) { List a1 = new ... Read More

2K+ Views
ArrayList doesn't have length() method, the size() method of ArrayList provides the number of objects available in the collection.Array has length property which provides the length or capacity of the Array. It is the total space allocated during the initialization of the array.

68 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.Exampleimport 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) { System.out.println("Value = " + value); } ... Read More

142 Views
A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ.Example:import java.util.*; public class SetDemo { public static void main(String args[]) { int count[] = {34, 22, 10, 60, 30, 22}; Set set = new HashSet(); try ... Read More

204 Views
The search(Object o) method is used to return the 1-based position where an object is on this stack.Exampleimport java.util.*; public class StackDemo { public static void main(String args[]) { Stack st = new Stack(); st.push("Java"); st.push("Source"); st.push("code"); System.out.println("Searching 'code' in stack: "+st.search("code")); } }OutputSearching 'code' in stack:

296 Views
The get(int index) method of the java.util.ArrayList class returns the element at the specified position in this list.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); for (Integer number : arrlist) { System.out.println("Number = " + number); } int retval=arrlist.get(3); System.out.println("Retrieved element is = " + retval); } }OutputNumber = 15 Number = 22 Number = 30 Number = 40 Retrieved element is = 40

68 Views
The empty() method is used to test if this stack is or not.Exampleimport java.util.*; public class StackDemo { public static void main(String args[]) { Stack st = new Stack(); st.push("Java"); st.push("Source"); st.push("code"); System.out.println("Is stack empty: "+st.empty()); } }OutputIs stack empty: false