Found 9326 Articles for Object Oriented Programming

Dump the content of an array in Java

Ankith Reddy
Updated on 25-Jun-2020 14:40:53

236 Views

An array can be easily printed by using the method java.util.Arrays.toString() in Java. This method returns a string representation of the array contents that is enclosed in square brackets. If the array is null, then this method returns null.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String args[]) {       String str[] = {"John", "Harry", "Sally", "Emma", "Peter"};       System.out.println("The array content is:");       System.out.println(Arrays.toString(str));    } }OutputThe array content is: [John, Harry, Sally, Emma, Peter]Now let us understand the above program.The ... Read More

Java Program to sort an array in case-insensitive order

George John
Updated on 25-Jun-2020 14:42:02

2K+ Views

An array can be sorted in case-insensitive order using the java.util.Arrays.sort() method. Also the java.text.Collator class is required as Collator.getInstance() is used to obtain the Collator object for the required locale.A program that demonstrates this is given as follows −Example Live Demoimport java.text.Collator; import java.util.Arrays; public class Demo {    public static void main(String args[]) {       String[] arr = new String[] { "apple", "mango", "Banana", "Melon", "orange" };       System.out.print("The unsorted array is: ");       System.out.println(Arrays.toString(arr));       Arrays.sort(arr, Collator.getInstance());       System.out.print("The sorted array in case-insensitive order is: ");     ... Read More

Get Synchronized List from ArrayList in java

Ankith Reddy
Updated on 12-Mar-2024 17:51:24

3K+ Views

In order to get a synchronized list from an ArrayList, we use the synchronizedList(List ) method in Java. The Collections.synchronizedList(List ) method accepts the ArrayList as an argument and returns a thread safe list.Declaration −The Collections.synchronizedList(List ) method is declared as follows −public static List synchronizedList(List list)Let us see a program to get a synchronized List from ArrayList −Example import java.util.*; public class Example { public static void main (String[] args) { List list = new ArrayList(); list.add("Hello"); ... Read More

How to schedule tasks in Java to run for repeated fixed-delay execution, beginning after the specified delay

Arjun Thakur
Updated on 25-Jun-2020 14:43:41

974 Views

One of the methods in the Timer class is the void schedule(TimerTask task, long delay, long period) method. This method schedules tasks for repeated fixed-delay execution, beginning after the specified delay.In fixed-delay execution, each execution is scheduled with respect to the original execution time of the preceding execution. If an execution is delayed for a particular reason (case in point, garbage collection), the subsequent executions will be delayed as well.Declaration −The java.util.Timer.schedule(TimerTask task, long delay, long period) is declared as follows −public void schedule(TimerTask task, long delay, long period)Here, task is the task to be scheduled, delay is the delay ... Read More

Java Program to implement Binary Search on double array

Arjun Thakur
Updated on 25-Jun-2020 14:45:34

215 Views

Binary search on a double array can be implemented by using the methodjava.util.Arrays.binarySearch(). This method returns the index of the required double element if it is available in the array, otherwise it returns (-(insertion point) - 1) where the insertion point is the position at which the element would be inserted into the array.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       double d_arr[] = { 5.2, 7.5, 9.7, 1.8, 4.0 };       Arrays.sort(d_arr);       System.out.print("The sorted array is: ... Read More

Check if the String contains only unicode letters, digits or space in Java

Chandu yadav
Updated on 25-Jun-2020 14:47:31

735 Views

To check if a given String contains only unicode letters, digits or space, we use the isLetterOrDigit() and charAt() methods with decision making statements.The isLetterOrDigit(char ch) method determines whether the specific character (Unicode ch) is either a letter or a digit. It returns a boolean value, either true or false.Declaration −The java.lang.Character.isLetter() method is declared as follows −public static boolean isLetter(char ch)The charAt() method returns a character value at a given index. It belongs to the String class in Java. The index must be between 0 to length()-1.Declaration −The java.lang.String.charAt() method is declared as follows −public char charAt(int index)Let us ... Read More

Convert a Queue to a List in Java

Nancy Den
Updated on 25-Jun-2020 14:48:19

1K+ Views

In order to convert a Queue to a List in Java, we can create an LinkedList and pass the Queue as an argument in the parameterized constructor of an ArrayList. This can be done as follows −Queue q = new LinkedList(); List l = new ArrayList(q);The quickest way is used to LinkedList in the first place which can be used both as a List and a Queue. This can be done as follows −Queue q = new LinkedList(); List l = (List) q;Let us see a program to convert a queue to a list −Example Live Demoimport java.util.LinkedList; import java.util.List; import ... Read More

Roll a six-sided die 6000 times in Java

Krantik Chavan
Updated on 25-Jun-2020 14:51:04

545 Views

In order to roll a six sided die 6000 times in Java, we need to the nextInt() statement with decision making statements.The nextInt() method returns the next random integer value from this random number generator sequence.Declaration − The java.util.Random.nextInt() method is declared as follows −public int nextInt()Let us see a program to roll a six sided die 6000 times −Example Live Demoimport java.util.Random; public class Example {    public static void main(String args[]) {       Random rd = new Random(); // random number generator       int freq[] = new int[6]; // creating an array to compute frequency ... Read More

Generate a random array of integers in Java

Anvi Jain
Updated on 12-Sep-2023 03:29:56

33K+ Views

In order to generate random array of integers in Java, we use the nextInt() method of the java.util.Random class. This returns the next random integer value from this random number generator sequence.Declaration − The java.util.Random.nextInt() method is declared as follows −public int nextInt()Let us see a program to generate a random array of integers in Java −Example Live Demoimport java.util.Random; public class Example {    public static void main(String[] args) {       Random rd = new Random(); // creating Random object       int[] arr = new int[5];       for (int i = 0; i

Generate Random bytes in Java

Smita Kapse
Updated on 25-Jun-2020 14:52:19

7K+ Views

In order to generate random bytes in Java, we use the nextBytes() method. The java.util.Random.nextBytes() method generates random bytes and provides it to the user defined byte array.Declaration − The java.util.Random.nextBytes() method is declared as follows −public void nextBytes(byte[] bytes)where bytes is the byte array.Let us see a program to generate random bytes in Java −Example Live Demoimport java.util.Random; public class Example {    public static void main(String[] args) {       Random rd = new Random();       byte[] arr = new byte[7];       rd.nextBytes(arr);       System.out.println(arr);    } }Output[B@15db9742Note − The output might ... Read More

Advertisements