Сообщения

Сообщения за июль, 2024

DSA: Arrays

Изображение
1. Best Score:  Given an array, write a function to get first, second best scores from the array and return it in new array. myArray = { 84 , 85 , 86 , 87 , 85 , 90 , 85 , 83 , 23 , 45 , 84 , 1 , 2 , 0 } firstSecond ( myArray ) // {90, 87}   package test; import java.util.Arrays; public class Main { public static void main (String[] args) { int [] arr = { 5 , 91 , 15 , 3 , 90 , 8 , 79 , 91 , 13 }; System. out .println(Arrays. toString ( findTopTwoScores (arr))); } public static int [] findTopTwoScores ( int [] array) { int [] result = new int [ 2 ]; int first = array[ 0 ]; int second = first; for ( int i = 1 ; i < array. length ; i++) { if (array[i] > first) { second = first; first = array[i]; } else if (array[i] > second && array[i] < first) { second = array[i]; } } result[ 0 ] = first; result[ 1 ]

DSA: Big O Notation

Изображение
1. Time Complexity  2. Space Complexity

Generics questions

 Question 1: package generics; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { public static void main (String[] args) { List<?> list = Arrays. asList ( 1 , "hello" , 2.2 ); List<Object> list2 = list; } // public static void reverse(List<?> list) { // List<Object> tmp = new ArrayList<>(list); // for (int i = 0; i < list.size(); i++) { // list.set(i, tmp.get(list.size() - i - 1)); // compile-time error } // } // } public static void foo2 (List<?> list) { List<?> tmp = new ArrayList<>(list); for ( int i = 0 ; i < list.size(); i++) { list.set(i, tmp.get(list.size() - i - 1 )); } } public static void foo (List<?> list) { rev (list); } private static < T > void rev (List< T > list) { List< T > tmp = new ArrayList<>(list

MultiThreading

Изображение
    Multithreading in Java refers to the concurrent execution of multiple threads within a java program. A thread is a lightweight sub-process, and multithreading allows you to perform multiple tasks simultaneously, improving the overall efficiency of your program.    Thread basics: package test; public class Main { public static void main (String[] args) { Thread currentThread = Thread. currentThread (); System. out .println(currentThread.getClass().getName()); printThreadState (currentThread); } public static void printThreadState (Thread thread) { System. out .println( "--------------------------------------------" ); System. out .println( "Thread ID: " + thread.getId()); System. out .println( "Thread Name: " + thread.getName()); System. out .println( "Thread Priority:" + thread.getPriority()); System. out .println( "Thread State: " + thread.getState());