To sort long array in java use Arrays.sort() method of java.util package. Java arrays are a data structure that is used to store data variables of the same type. These data variables are sequential collections of elements. Instead of declaring each variable individually, one variable is declared which can hold a collection of all variables that will be used by the program.
Array declaring syntax
String[] colors;
Declaring Array with initialization
String[] iPhoneColors = {"Red", "Yellow", "Purple", "Black", "Green", "White"};
Access array elements
String[] iPhoneColors = {"Red", "Yellow", "Purple", "Black", "Green", White"}; System.out.println(iPhoneColors[0]); // Outputs Red
Modify Array Elements.
String[] iPhoneColors = {"Red", "Yellow", "Purple", "Black", "Green", White"}; iPhoneColors[0] = "Bottle Green"; System.out.println(iPhoneColors[0]); // Outputs Bottle Green
Find the number of elements in an array
String[] iPhoneColors = {"Red", "Yellow", "Purple", "Black", "Green", White"}; System.out.println(iPhoneColors.length); //Outputs 6 String[] iPhoneColors = {"Red", "Yellow", "Purple", "Black", "Green", White"}; iPhoneColors[0] = "Bottle Green"; System.out.println(iPhoneColors.length); //Outputs 6
Sort Long Arrays
There are two methods available in java.util.Arrays class to sort the long array. One is sort full and the other is to sort a partial array by specifying the start and end index.
Here is an example that shows how to sort in ascending order.
package com.rsmrocks.utils; import java.util.Arrays; public class SortLongArrayDemo { public static void main(String[] args) { //long array long[] longArr = new long[]{54,45,23,89,12}; //print original input long array System.out.print("Original Input Array :"); for(long longnumber : longArr) { System.out.print(" " + longnumber); } //Use sort(long[] l) method from java.util package to sort Array. Arrays.sort(longArr); //print sorted output long array System.out.print("\nSorted Output array :"); for(long longnumber : longArr) { System.out.print(" " + longnumber); } } }
The output of the above example is as follows:
- Original Input Array : 54 45 23 89 12
- Sorted Output array : 12 23 45 54 89
Here is an example that shows how to sort partially in Java.
package com.rsmrocks.utils; import java.util.Arrays; public class SortLongArrayPartialDemo { public static void main(String[] args) { //long array long[] longArr = new long[]{54,45,23,89,12}; //print original input long array System.out.print("Original Input Array :"); for(long longnumber : longArr) { System.out.print(" " + longnumber); } //Use sort(long[] l, int fromIndex, int toIndex) method from java.util package to sort Array. Arrays.sort(longArr, 0, 3); //print partial sorted output long array System.out.print("\nPartial Sorted Output : "); for(long longnumber : longArr) { System.out.print(" " + longnumber); } } }
The output of the above example is as follows:
- Original Input Array : 54 45 23 89 12
- Partial Sorted Output : 23 45 54 89 12
For the partial sort method, you need to specify the ‘from index’ and ‘to index’. Here ‘from index’ is the index of the first element, inclusive, to be sorted and ‘to index’ is the index of the last element, exclusive, to be sorted.
Additional Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
You can also check my another Article explaining Microservices – https://procodeguide.com/programming/microservices-architecture/