0% found this document useful (0 votes)
4 views2 pages

Java ArrayList Operations Guide

The document demonstrates creating an ArrayList in Java, adding and removing elements, sorting the list, and converting it to an array. It creates an ArrayList of Integers, adds elements, removes one, sorts the list, and converts it to an array using toArray().

Uploaded by

Raghu Nandan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Java ArrayList Operations Guide

The document demonstrates creating an ArrayList in Java, adding and removing elements, sorting the list, and converting it to an array. It creates an ArrayList of Integers, adds elements, removes one, sorts the list, and converts it to an array using toArray().

Uploaded by

Raghu Nandan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Implement a java program to demonstrate creating an ArrayList, adding elements,


removing elements, Sorting elements of ArrayList. Also illustrate the use of toArray()
method.
import [Link];

import [Link];

public class ArrayListDemo {

public static void main(String[] args) {

// Creating an ArrayList

ArrayList<Integer> numbers = new ArrayList<>();

// Adding elements to the ArrayList

[Link](5);

[Link](3);

[Link](8);

[Link](1);

// Displaying the ArrayList before sorting

[Link]("ArrayList before sorting: " + numbers);

// Removing an element from the ArrayList

[Link](2);

// Displaying the ArrayList after removing an element

[Link]("ArrayList after removing an element: " + numbers);

// Sorting the elements of the ArrayList

[Link](numbers);

// Displaying the ArrayList after sorting

[Link]("ArrayList after sorting: " + numbers);

// Using toArray() method to convert ArrayList to array

Integer[] array = [Link](new Integer[[Link]()]);


// Displaying the array

[Link]("Array from ArrayList: ");

for (int num : array) {

[Link](num + " ");

You might also like