# Archives: Logical Programs

## ArrayList partition into two lists of odd/even numbers

Problem Statement: Write a Java program to create a method that partitions an ArrayList of integers into two separate lists, one containing even numbers and the other containing odd numbers. Here’s a Java program that demonstrates how to create a method to partition an ArrayList of integers into two separate lists, one containing even numbers …

## Find common elements in two ArrayLists in Java

Problem Statement: Write a Java program to find the common elements in two ArrayLists. Here’s a Java program that demonstrates how to find common elements in two ArrayLists. OUTPUT: List 1: [10, 20, 30, 40]List 2: [20, 40, 60, 80]Common Elements: [20, 40] Explanation: By using HashSet to store unique elements and take advantage of …

## Remove elements with Condition: preserve the original order

Problem Statement: Write a Java program to remove duplicates from an ArrayList while preserving the original order. Here’s a Java program that demonstrates how to remove duplicates from an ArrayList while preserving the original order. I’ll provide an explanation of the program’s logic. OUTPUT: Original List: [10, 20, 30, 20, 40, 10]List after removing duplicates: …

## Remove elements with Condition: greater than a certain value

Problem Statement: Write a Java program to remove all elements from an ArrayList that are greater than a certain value. Here’s a Java program that demonstrates how to remove all elements from an ArrayList that are greater than a certain value. I’ll provide an explanation of the program’s logic. OUTPUT: Original List: [10, 25, 5, …

## List to Array Conversion

Problem Statement: Write a Java program to Convert an ArrayList of strings into a string array. Here’s a Java program that demonstrates how to convert an ArrayList of strings into a string array. I’ll provide an explanation of the program’s logic. OUTPUT: String Array:HelloWorldJavaProgramming Explanation: The toArray() method converts the ArrayList of strings into a …

## Create a stack using an ArrayList

Problem Statement: Create a stack using an ArrayList and implement push, pop, and peek operations. Here’s a Java program that demonstrates how to create a stack using an ArrayList and implements the push, pop, and peek operations. I’ll provide an explanation of the program’s logic. OUTPUT: Peek: 30Stack size: 3Pop: 30Pop: 20Is stack empty? false …

## Sorting a List: sort an ArrayList of custom objects

Problem Statement: Implement a Java program to sort an ArrayList of custom objects based on a specific attribute. Here’s a Java program that demonstrates how to sort an ArrayList of custom objects (in this case, Employee objects) based on a specific attribute (name in this case). I’ll provide an explanation of the program’s logic. OUTPUT: …

## Sorting a List: Sort an ArrayList of integers

Problem Statement: Implement a Java program to sort an ArrayList of integer numbers. Here’s a Java program that demonstrates how to sort an ArrayList of integers using the built-in Collections.sort() method. I’ll provide you with an explanation of the program’s logic. OUTPUT: Original List: [5, 2, 8, 1, 3]Sorted List: [1, 2, 3, 5, 8] …

## Find duplicate elements in an ArrayList

Problem Statement: Implement a method to find duplicate elements in an ArrayList in Java. Here’s a Java program that demonstrates how to implement a method to find duplicate elements in an ArrayList. I’ll provide a step-by-step explanation of the logic used in the program. OUTPUT: Original List: [1, 2, 3, 2, 4, 1]Duplicate Numbers: [1, …

## List Manipulation: Reverse a List

Problem Statement: Implement a Java program to reverse a List without using the built-in Collections.reverse() method. Here’s a Java program that reverses a list without using the built-in Collections.reverse() method. I’ll provide a step-by-step explanation of the logic used in the program. OUTPUT: Original List: [1, 2, 3, 4, 5]Reversed List: [5, 4, 3, 2, …