0% found this document useful (0 votes)
7 views

Java 1 Assign

Uploaded by

dryoplayz
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)
7 views

Java 1 Assign

Uploaded by

dryoplayz
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/ 4

TYCOA061

Assignment 1:
1.1) Write a Java program to remove the third element from an arraylist.
CODE:
import java.util.ArrayList;
import java.util.Scanner;

public class RemoveThirdElement_1 {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

// Create an ArrayList object to store the elements


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

System.out.println("How many elements do you want to enter: ");


int n = sc.nextInt();

// Loop through n times and add each element


System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
int element = sc.nextInt();
list.add(element);
}
System.out.println("Original list: " + list);

// Check if the list has at least three elements


if (list.size() >= 3) {
// Remove the third element from the list
list.remove(2);

// Print the modified list


System.out.println("Modified list: " + list);
} else {
// Print a message that the list is too small
System.out.println("The list is too small to remove the third
element.");
}

// Close the Scanner object


sc.close();
}
}

OUTPUT:
How many elements do you want to enter: 6
Enter 6 elements:
1 2 4 5 8 6
Original list: [1, 2, 4, 5, 8, 6]
Modified list: [1, 2, 5, 8, 6]
1.2) Write a Java program to remove the third element from a linked list.

CODE:

import java.util.LinkedList;
import java.util.Scanner;

public class RemoveThirdElement_2 {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

// Create a LinkedList object to store the elements


LinkedList<Integer> list = new LinkedList<>();

System.out.println("How many elements do you want to enter:");


int n = sc.nextInt();

// Loop through n times and add each element


System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
int element = sc.nextInt();
list.add(element);
}
System.out.println("Original list: " + list);

// Check if the list has at least three elements


if (list.size() >= 3) {
// Remove the third element from the list
list.remove(2);

// Print the modified list


System.out.println("Modified list: " + list);
} else {
// Print a message that the list is too small
System.out.println("The list is too small to remove the third
element.");
}

// Close the Scanner object


sc.close();
}
}

OUTPUT:
How many elements do you want to enter: 5
Enter 5 elements:
1 2 4 5 8
Original list: [1, 2, 4, 5, 8]
Modified list: [1, 2, 5, 8]
1.3) Write a Java program to remove the third element from a Sets.

CODE:

import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;

public class RemoveThirdElement_3 {

public static void main(String[] args) {


// Create a Scanner object to read input from the user
Scanner sc = new Scanner(System.in);

// Create a HashSet object to store the elements


HashSet<Integer> set = new HashSet<>();

System.out.println("How many elements do you want to enter: ");


int n = sc.nextInt();

// Loop through n times and add each element to the set


System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
int element = sc.nextInt();
set.add(element);
}

// Print the original set


System.out.println("Original set: " + set);

// Check if the set has at least three elements


if (set.size() >= 3) {
// Create an iterator to traverse the set
Iterator<Integer> it = set.iterator();

// Skip the first two elements


it.next();
it.next();

// Remove the third element from the set


it.remove();

// Print the modified set


System.out.println("Modified set: " + set);
} else {
// Print a message that the set is too small
System.out.println("The set is too small to remove the third
element.");
}
// Close the Scanner object
sc.close();
}
}

OUTPUT:

How many elements do you want to enter: 6


Enter 6 elements:
1 4 5 9 2 6
Original set: [1, 2, 4, 5, 6, 9]
Modified set: [1, 4, 5, 6, 9]

You might also like