Untitled
unknown
plain_text
a year ago
3.5 kB
17
Indexable
1
package arraylistprogram;
import java.util.ArrayList;
import java.util.Collections;
public class sample {
public static void main(String[] args) { // Creating an ArrayList
ArrayList<Integer> numbers = new ArrayList<Integer>(); // Adding elements to the ArrayList
numbers.add(5);
numbers.add(2);
numbers.add(7);
numbers.add(3);
numbers.add(1);
// Removing an element from the ArrayList
System.out.println("ArrayList after adding elements: " + numbers);
numbers.remove(2); // Removes the element at index 2
// Sorting the elements of the ArrayList
System.out.println("ArrayList after removing element at index 2: " + numbers);
Collections.sort(numbers);
// Converting ArrayList to Array using toArray() method
System.out.println("ArrayList after sorting: " + numbers);
Integer[] numbersArray = numbers.toArray(new Integer[numbers.size()]);
System.out.println("Array after converting from ArrayList: ");
for (Integer num : numbersArray) {
System.out.print(num + " ");
}
}
}
2
package comparatorprogram;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
class TensPlaceComparator implements Comparator<Integer> {
public int compare(Integer num1, Integer num2) {
int tensPlace1 = (num1 % 100) / 10;
int tensPlace2 = (num2 % 100) / 10;
return Integer.compare(tensPlace1, tensPlace2);
}
}
public class RandomNumbersSort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the lower bound of the range: ");
int lowerBound = scanner.nextInt();
System.out.print("Enter the upper bound of the range: ");
int upperBound = scanner.nextInt();
System.out.print("Enter the number of random numbers to generate: ");
int count = scanner.nextInt();
List<Integer> randomNumbers = generateRandomNumbers(lowerBound, upperBound,
count);
System.out.println(randomNumbers);
Collections.sort(randomNumbers, new TensPlaceComparator());
System.out.println("\nSorted Numbers According to Tens Place:");
for (int num : randomNumbers) {
System.out.println(num);
}
scanner.close();
}
private static List<Integer> generateRandomNumbers(int lowerBound, int upperBound, int count) {
List<Integer> randomNumbers = new ArrayList<>();
Random rand = new Random();
int num;
int i=0;
while(i<count) {
num = rand.nextInt(upperBound - lowerBound + 1) + lowerBound;
if (num % 2 == 0 && num % 5 == 0) {
randomNumbers.add(num);
i++;
}
}
return randomNumbers;
}
}
3
import java.util.ArrayList;
import java.util.List;
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class Main {
public static void main(String[] args) {
// Create a list to store Person objects
List<Person> personList = new ArrayList<>();
// Adding some Person objects to the list
personList.add(new Person("Alice", 30));
personList.add(new Person("Bob", 25));
personList.add(new Person("Charlie", 35));
// Displaying the contents of the list
System.out.println("List of Persons:");
for (Person person : personList) {
System.out.println(person);
}
}
}Editor is loading...
Leave a Comment