Sorting float array in Java example

by Chaitanya Singh July 24, 2014

In this tutorial we are gonna learn how to sort a float array. import java.util.Arrays; class SortingFloatArrayExample { public static void main(String[] args) { // Creating a Float Array float[] floatArray = new float[] { 21.1f, 9.9f, 9.8f, 7.5f, 2.1f }; // Displaying Array before Sorting System.out.println("**Float Array Before Sorting**"); for (float temp: floatArray){ System.out.println(temp); } // Sorting the Array […]

Read the full article →

Sorting double array in Java example

by Chaitanya Singh July 24, 2014

import java.util.Arrays; class SortingDoubleArray { public static void main(String[] args) { // Creating a Double Array double[] doubleArray = new double[] { 13.1, 2.5, 2.2, 41.1, 1.1 }; // Displaying Array before Sorting System.out.println("**Double Array Before Sorting**"); for (double d: doubleArray){ System.out.println(d); } // Sorting the Array Arrays.sort(doubleArray); System.out.println("**Double Array After Sorting**"); for (double d: doubleArray){ System.out.println(d); } // Another […]

Read the full article →

Sorting char array in Java example

by Chaitanya Singh July 24, 2014

In this example we are sorting a char array. We have demonstrated two types of sorting in the program 1) Complete sorting using sort(char[] a) method 2) Sorting specified range of characters only using sort(char[] a, int fromIndex, int toIndex) method. import java.util.Arrays; class SortCharArray { public static void main(String[] args) { // Creating a Char Array char[] charArray = […]

Read the full article →

Sort byte array in Java

by Chaitanya Singh July 24, 2014

This is the example of sorting a byte array in Java. Here we have done two types of sorting 1) complete sorting using sort(byte[] a) method 2) Selective sorting using sort(byte[] a, int fromIndex, int toIndex) method – It sorts the specified range only. Refer comments in the program for more detail. import java.util.Arrays; class SortByteArray { public static void […]

Read the full article →

Java – Finding minimum and maximum values in an array

by Chaitanya Singh July 24, 2014

In this example we are finding out the maximum and minimum values from an int array. class MinMaxExample { public static void main(String args[]){ int array[] = new int[]{10, 11, 88, 2, 12, 120}; // Calling getMax() method for getting max value int max = getMax(array); System.out.println("Maximum Value is: "+max); // Calling getMin() method for getting min value int min = […]

Read the full article →

Random shuffling of an array in Java

by Chaitanya Singh July 24, 2014

import java.util.Arrays; import java.util.Collections; import java.util.List; class ShuffleArrayExample { public static void main(String[] args) { // String Array String[] stringArray = new String[] { "FF", "PP", "AA", "OO", "DD" }; // converting array to a List List<String> list = Arrays.asList(stringArray); // Shuffling list elements Collections.shuffle(list); System.out.println("String Array: "); for (String str : list) { System.out.println(str); } Integer[] intArray = new […]

Read the full article →

Sort an Array in Descending (Reverse) Order – Java

by Chaitanya Singh July 24, 2014

The previous tutorial was all about sorting an array in ascending order. In this post we are going to learn how to sort an array in Descending (Reverse) Order. Example Here we have two arrays, one is integer array and another one is String array. We are sorting both the arrays in reverse order. import java.util.Arrays; import java.util.Collections; class SortArrayExample […]

Read the full article →

How to sort an Array in Java

by Chaitanya Singh July 23, 2014

This is the example of sorting an Array in java. Here we have two arrays, one is an integer array and another one is a String array and we are sorting both of them using Arrays.sort() method. import java.util.Arrays; class SortArrayExample { public static void main(String[] args) { // int Array int[] intArr = new int[] { 14, 16, 3, […]

Read the full article →

Final method parameters in java

by Chaitanya Singh July 23, 2014

In the last tutorial we discussed about final keyword. In this post we are gonna discuss about final method parameters. You must have seen the use of final keyword in method arguments. Lets take an example to understand it: class FinalDemo { public void myMethod(int num, final String str){ // This is allowed as num is not final num = […]

Read the full article →

How to remove only trailing spaces of a string in Java

by Chaitanya Singh July 23, 2014

In this tutorial we will learn how to trim trailing spaces from the string but not leading spaces. Here is the complete code: class TrimBlanksExample { public static void main(String[] args) { System.out.println("#"+trimTrailingBlanks(" How are you?? ")+"@"); System.out.println("#"+trimTrailingBlanks(" I'm Fine. ")+"@"); } public static String trimTrailingBlanks( String str) { if( str == null) return null; int len = str.length(); for( […]

Read the full article →

java – Left padding a String with Spaces and Zeros

by Chaitanya Singh July 23, 2014

In this tutorial we are gonna see how to left pad a string with spaces and zeros: 1) Left pad with spaces class LeftPaddingExample1 { public static void main(String[] args) { System.out.println("#" + padLeftSpaces("mystring", 10) + "@"); System.out.println("#" + padLeftSpaces("mystring", 15) + "@"); System.out.println("#" + padLeftSpaces("mystring", 20) + "@"); } public static String padLeftSpaces(String str, int n) { return String.format("%1$" […]

Read the full article →

Our Friend's website link : FromDev.com