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

Java Assign1

Uploaded by

prasannakotkar7
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)
10 views2 pages

Java Assign1

Uploaded by

prasannakotkar7
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
You are on page 1/ 2

Assignment No 1

Name : Badal Madhukar Gaurkhede


Prn no : 122B1B085

Code :

import java.util.ArrayList;
import java.util.Scanner;

public class RemoveElement {


public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Date");
fruits.add("Elderberry");

System.out.println("Original ArrayList: " + fruits);

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the index of the element to remove (0 to " + (fruits.size() - 1) + "):


");

int index = scanner.nextInt();

if (index >= 0 && index < fruits.size()) {


String removedElement = fruits.remove(index);
System.out.println("Removed element: " + removedElement);
System.out.println("ArrayList after removal: " + fruits);
} else {
System.out.println("Invalid index. Please enter a number between 0 and " + (fruits.size()
- 1) + ".");
}
scanner.close();
}
}

Output :
java -cp /tmp/OBe61xb6TU/RemoveElement
Original ArrayList: [Apple, Banana, Cherry, Date, Elderberry]
Enter the index of the element to remove (0 to 4): 3
Removed element: Date
ArrayList after removal: [Apple, Banana, Cherry, Elderberry]

=== Code Execution Successful ===

You might also like