Found 10102 Articles for Object Oriented Programming

What does the method removeRange(int fromIndex, int toIndex) do in java?

Srinivas Gorla
Updated on 20-Feb-2020 12:24:27

54 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]

What does the method set(int, obj o) do in java?

Abhinanda Shri
Updated on 20-Feb-2020 12:22:09

54 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

What does the method listIterator(n) do in java?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:21

24 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

What does the method trimToSize() do in java?

Abhinaya
Updated on 20-Feb-2020 12:21:01

83 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

How to make Java ArrayList read only?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:21

79 Views

You can make a collection unmodifiable using the unmodifiableList() method of the Collections interface. Example Live Demo import java.util.ArrayList; import java.util.Collections; import java.util.List; 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"); System.out.println(list); List myList= (List) Collections.unmodifiableList(list); myList.add("Apache Hadoop"); System.out.println(myList); } } Output Exception in thread "main" [JavaFx, Java, WebGL, OpenCV] java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Unknown Source) at collections_package.Sample.main(Sample.java:16)

Difference Between ArrayList and LinkedList in Java?

Kiran Kumar Panigrahi
Updated on 12-Jul-2023 13:30:44

1K+ Views

Java developers frequently store and work with collections of data using the data structures ArrayList and LinkedList. They are better suited for certain use cases due to the differences in their underlying implementation and performance characteristics. In this tutorial, we will examine the performance, memory requirements, and usage situations of ArrayList and LinkedList as well as their differences. By the end of this tutorial, you will have a better knowledge of when to use ArrayList and when to use LinkedList in your Java projects. Arraylist in Java In Java, an ArrayList is a dynamic array data structure that enables ... Read More

Can a Vector contain heterogeneous objects in Java?

Ramu Prasad
Updated on 30-Jul-2019 22:30:21

595 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());    } }

What is the difference between size and capacity of a Vector in Java?

Sravani S
Updated on 30-Jul-2019 22:30:21

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

What are Java container objects like Vector and ArrayList?

Janani Jaganathan
Updated on 13-Oct-2022 11:43:17

362 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

What does the method ensureCapacity(int, minCapacity) do in java?

Govinda Sai
Updated on 25-Feb-2020 10:10:30

85 Views

The ensureCapacity(int minCapacity) method of the class java.util.ArrayList increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String args[]) {       ArrayList arrlist = new ArrayList(5);       arrlist.add(10);       arrlist.add(50);       arrlist.add(30);       arrlist.ensureCapacity(15);       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }    } }OutputNumber = 10 Number = 50 Number = 30

Advertisements