0% found this document useful (0 votes)
26 views5 pages

Java Assingment Nikhil Kumar 1CR23IS111

The document contains Java programs created by Nikhil Kumar that perform various tasks such as searching for an element in an array, converting an ArrayList to an array and finding the maximum element, sorting a linked list of integers, and creating a priority queue of random integers. Each program is structured with a main method that implements the required functionality using standard Java collections. The code demonstrates basic operations like searching, sorting, and using data structures effectively.

Uploaded by

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

Java Assingment Nikhil Kumar 1CR23IS111

The document contains Java programs created by Nikhil Kumar that perform various tasks such as searching for an element in an array, converting an ArrayList to an array and finding the maximum element, sorting a linked list of integers, and creating a priority queue of random integers. Each program is structured with a main method that implements the required functionality using standard Java collections. The code demonstrates basic operations like searching, sorting, and using data structures effectively.

Uploaded by

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

Name – Nikhil Kumar

usn – 1cr23is111

//wap that checks if a certain element exists in an array list,

//if the element is found print its index otherwise print element not found

package javaAssingmentNikhilKumar;

import java.util.Scanner;

public class program1 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int[] arr = {10, 20, 30, 40, 50};

System.out.print("Enter the element to search: ");

int key = sc.nextInt();

int index = -1;

for (int i = 0; i < arr.length; i++) {

if (arr[i] == key) {

index = i;

break;

if (index != -1) {

System.out.println("Element found at index: " + index);

} else {

System.out.println("Element not found");


}

sc.close();

//wap that converts array list

//of integers into an array and then finds max elements in the array and print the max element

package javaAssingmentNikhilKumar;

import java.util.*;

public class Program2 {

public static void main (String[] args) {

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

al.add(10);

al.add(25);

al.add(5);

al.add(40);

al.add(15);

Integer[] arr = new Integer[al.size()];

arr = al.toArray(arr);

int max = arr[0];

for (int i = 1; i < arr.length; i++) {

if (arr[i] > max) {

max = arr[i];
}

System.out.println("Maximum element in the array: " + max);

//wap to sort a linked list of integers in ascending order and print the sorted list

package javaAssingmentNikhilKumar;

import java.util.*;

public class program3 {

public static void main(String[] args) {

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

list.add(40);

list.add(10);

list.add(30);

list.add(20);

list.add(50);

Integer[] arr = list.toArray(new Integer[0]);

for (int i = 0; i < arr.length - 1; i++) {

for (int j = 0; j < arr.length - i - 1; j++) {

if (arr[j] > arr[j + 1]) {


int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

System.out.print("Sorted Array: ");

for (int num : arr) {

System.out.print(num + " ");

//wap to create a priority queue of integers and add 10 random numbers to the queue and print
the elements to

//the queue and ensure that elements are printed in ascending order(natural order)

package javaAssingmentNikhilKumar;

import java.util.PriorityQueue;

import java.util.Random;

public class Program4 {

public static void main(String[] args) {


PriorityQueue<Integer> pq = new PriorityQueue<>();

Random rand = new Random();

for (int i = 0; i < 10; i++) {

pq.add(rand.nextInt(100));

System.out.println("Priority Queue elements in ascending order:");

while (!pq.isEmpty()) {

System.out.print(pq.poll() + " ");

You might also like