Found 2617 Articles for Java

Java program to sort words of sentence in ascending order

AmitDiwan
Updated on 07-Jul-2020 09:41:56

5K+ Views

To sort words of sentence in ascending order, the Java code is as follows −Example Live Demoimport java.util.*; public class Demo{    static void sort_elements(String []my_str, int n){       for (int i=1 ;i= 0 && temp.length() < my_str[j].length()){             my_str[j+1] = my_str[j];             j--;          }          my_str[j+1] = temp;       }    }    public static void main(String args[]){       String []my_arr = {"This", "is", "a", "sample"};       int len = my_arr.length;       sort_elements(my_arr,len);       System.out.print("The sorted array is : ");       for (int i=0; i

Java program to split the Even and Odd elements into two different lists

AmitDiwan
Updated on 07-Jul-2020 09:16:21

724 Views

To split the Even and Odd elements into two different lists, the Java code is as follows −Example Live Demoimport java.util.Scanner; public class Demo{    public static void main(String[] args){       int n, j = 0, k = 0;       Scanner s = new Scanner(System.in);       System.out.println("Enter the number of elements required :");       n = s.nextInt();       int my_arr[] = new int[n];       int odd_vals[] = new int[n];       int even_vals[] = new int[n];       System.out.println("Enter the elements of the array(even and add numbers) ... Read More

Java program to find missing and additional values in two lists

AmitDiwan
Updated on 07-Jul-2020 08:36:21

1K+ Views

To find missing and additional values in two lists, the Java program is as follows −Example Live Demoimport java.util.*; public class Demo{    public static void main(String[] args){       List my_list_1 = new ArrayList();       List my_list_2 = new ArrayList();       my_list_1.add(new Integer("101"));       my_list_1.add(new Integer("90"));       my_list_1.add(new Integer("34"));       my_list_2.add(new Integer("34"));       my_list_2.add(new Integer("67"));       my_list_2.add(new Integer("90"));       for(int i = 0; i < my_list_1.size(); i++){          if (my_list_2.contains(my_list_1.get(i)))          continue;           ... Read More

Java program to generate random numbers within a given range and store in a list

AmitDiwan
Updated on 07-Jul-2020 09:15:25

291 Views

To generate random numbers in a given range, the Java code is as follows −Example Live Demoimport java.util.Random; import java.util.*; public class Demo{    public static void main(String args[]){       Random my_rand = new Random();       List my_list_1 = new ArrayList();       int v_1 = my_rand.nextInt(1000);       int v_2 = my_rand.nextInt(967);       int v_3 = my_rand.nextInt(1050);       int v_4 = my_rand.nextInt(10000);       int v_5 = my_rand.nextInt(100);       my_list_1.add(v_1);       my_list_1.add(v_2);       my_list_1.add(v_3);       my_list_1.add(v_4);       my_list_1.add(v_5);   ... Read More

Java program to split and join a string

AmitDiwan
Updated on 07-Jul-2020 08:28:09

3K+ Views

To split and join a string in Java, use the split() and join() method as in the below example −Examplepublic class Demo{    public static void main(String args[]){       String my_str = "This_is_a_sample";       String[] split_str = my_str.split("_", 4);       System.out.println("The split string is:");       for (String every_Str : split_str)       System.out.println(every_Str);       String joined_str = String.join("_", "This", "is", "a", "sample");       System.out.println("The joined string is:");       System.out.println(joined_str);    } }OutputThe split string is: This is a sample The joined string is: This_is_a_sampleA class ... Read More

Convert one base to other bases in a single Java Program

AmitDiwan
Updated on 07-Jul-2020 08:27:06

890 Views

Let’s say we have an Octal number. To convert Octal to other bases like binary, hexadecimal, etc, the Java code is as follows −Example Live Demopublic class Demo{    public static String base_convert(String num, int source, int destination){       return Integer.toString(Integer.parseInt(num, source), destination);    }    public static void main(String[] args){       String my_num = "345";       int source = 8;       int destination = 2;       System.out.println("Converting the number from octal to binary: "+ base_convert (my_num, source, destination));       destination = 10;       System.out.println("Converting the number ... Read More

How to write an empty function in Java

AmitDiwan
Updated on 07-Jul-2020 08:24:48

2K+ Views

Let us see how to write an empty function in Java −Example Live Demoimport java.util.Vector; public class Demo{    public static void my_empty_fun(){    }    public static void main(String[] args){       System.out.println("In the main function");       my_empty_fun();    }   }OutputIn the main functionAn empty function is basically creating a function without defining any operations inside it. A class named Demo contains an empty function named ‘my_empty_fun’ which is just completed by placing two flower brackets, without adding any functionality into it. In the main function, a print statement is written after which the empty function ... Read More

Run-time Stack mechanism in Java

AmitDiwan
Updated on 07-Jul-2020 08:23:32

736 Views

Eeverytime a process or a code or a thread needs to run in Java, a runtime stack is created so as to store the operations performed while executing the thread.Every entry in the run-time stack is known as stack frame or activation record. Once a function has been called by the process, its associated data is deleted from the runtime stack.Once all the functions have been called, the runtime stack will be empty. This means it needs to be removed from the memory.At this point in time, the runtime stack is destroyed and then the thread is also terminated.A termination ... Read More

Merge two sets in Java

AmitDiwan
Updated on 07-Jul-2020 08:21:48

2K+ Views

To merge two sets in Java, the code is as follows −Example Live Demoimport java.util.stream.*; import java.util.*; import java.io.*; public class Demo{    public static Set set_merge(Set set_1, Set set_2){       Set my_set = set_1.stream().collect(Collectors.toSet());       my_set.addAll(set_2);       return my_set;    }    public static void main(String[] args){       Set my_set_1 = new HashSet();       my_set_1.addAll(Arrays.asList(new Integer[] { 34, 67, 89, 102 }));       Set my_set_2 = new HashSet();       my_set_2.addAll(Arrays.asList(new Integer[] { 77, 11, 0 , -33}));       System.out.println("The first set contains " ... Read More

Merge arrays into a new object array in Java

AmitDiwan
Updated on 07-Jul-2020 08:20:12

264 Views

Following is the Java program to merge array into a new object array in Java −Example Live Demoimport java.util.stream.Stream; import java.util.Arrays; import java.io.*; public class Demo{    public static Object[] concat_fun(T[] my_obj_1, T[] my_obj_2){       return Stream.concat(Arrays.stream(my_obj_1), Arrays.stream(my_obj_2)).toArray();    }    public static void main (String[] args){       Integer[] my_obj_1 = new Integer[]{67, 83, 90};       Integer[] my_obj_2 = new Integer[]{11, 0, 56};       Object[] my_obj_3 = concat_fun(my_obj_1, my_obj_2);       System.out.println("The two objects merged into a single object array : " +  Arrays.toString(my_obj_3));    } }OutputThe two objects merged into ... Read More

Advertisements