- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 34494 Articles for Programming
238 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
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
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
989 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
217 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
740 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
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
550 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
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
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