0% found this document useful (0 votes)
2 views1 page

1-ArrayList

The document presents a Java program that demonstrates the use of an ArrayList. It includes operations such as adding, sorting, and removing elements, as well as converting the ArrayList to an array using the toArray() method. The output shows the state of the ArrayList before and after sorting and removing an element.

Uploaded by

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

1-ArrayList

The document presents a Java program that demonstrates the use of an ArrayList. It includes operations such as adding, sorting, and removing elements, as well as converting the ArrayList to an array using the toArray() method. The output shows the state of the ArrayList before and after sorting and removing an element.

Uploaded by

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

import java.util.

ArrayList;
import java.util.Collections;

public class ArrayListDemo {


public static void main(String[] args) {
// Creating an ArrayList
ArrayList<Integer> numbers = new ArrayList<>();

// Adding elements to the ArrayList


numbers.add(5);
numbers.add(2);
numbers.add(7);
numbers.add(3);
numbers.add(1);

// Displaying the ArrayList before sorting


System.out.println("ArrayList before sorting: " + numbers);

// Sorting the elements of the ArrayList


Collections.sort(numbers);

// Displaying the ArrayList after sorting


System.out.println("ArrayList after sorting: " + numbers);

// Removing an element from the ArrayList


numbers.remove(2); // Removing the element at index 2

// Displaying the ArrayList after removing an element


System.out.println("ArrayList after removing an element: " + numbers);

// Illustrating the use of toArray() method


Integer[] numbersArray = numbers.toArray(new Integer[0]);

// Displaying the array obtained from toArray() method


System.out.println("Array obtained from toArray() method:");
for (Integer num : numbersArray) {
System.out.println(num);
}
}
}

O/P:

ArrayList before sorting: [5, 2, 7, 3, 1]


ArrayList after sorting: [1, 2, 3, 5, 7]
ArrayList after removing an element: [1, 2, 5, 7]
Array obtained from toArray() method:
1
2
5
7

You might also like