Java Assignment Vtu SMVIT
Java Assignment Vtu SMVIT
Java ArrayList class uses a dynamic array for storing the elements. It is like an
array, but there is no size limit. We can add or remove elements anytime. So, it
is much more flexible than the traditional array. It is found in
the java.util package.
1. import java.util.*;
2. public class ArrayListExample1{
3. public static void main(String args[]){
4. ArrayList<String> list=new ArrayList<String>();//Creating arraylist
5. list.add("Mango");//Adding object in arraylist
6. list.add("Apple");
7. list.add("Banana");
8. list.add("Grapes");
9. //Printing the arraylist object
10. System.out.println(list);
11. }
12. }
The LinkedList stores its items in "containers." The list has a link to the first
container and each container has a link to the next container in the list. To
add an element to the list, the element is placed into a new container and
that container is linked to one of the other containers in the list.
import java.util.LinkedList;
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars);
The Queue interface provides several methods for adding, removing, and
inspecting elements in the queue. Here are some of the most commonly
used methods:
add(element): Adds an element to the rear of the queue. If the queue is
full, it throws an exception.
offer(element): Adds an element to the rear of the queue. If the queue is
full, it returns false.
remove(): Removes and returns the element at the front of the queue. If
the queue is empty, it throws an exception.
poll(): Removes and returns the element at the front of the queue. If the
queue is empty, it returns null.
element(): Returns the element at the front of the queue without
removing it. If the queue is empty, it throws an exception.
peek(): Returns the element at the front of the queue without removing it.
If the queue is empty, it returns null.
import java.util.*;
pq.add("Geeks");
pq.add("For");
pq.add("Geeks");
System.out.println(pq);
}
}
import java.util.ArrayList;
import java.util.List;
class Person {
private String name;
private int age;
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}
}
The java collection framework often we want to cycle through the elements. For
example, we might want to display each element of a collection. The java provides
an interface Iterator that is available inside the java.util package to cycle through
each element of a collection.
🔔 The Iterator allows us to move only forward direction.
🔔 The Iterator does not support the replacement and addition of new elements.
We use the following steps to access a collection of elements using the Iterator.
}
}
o
o
o
o
o
o
o
o
o
o
o