- /*
- Java Sort char Array Example
- This example shows how to sort a char array using sort method of Arrays class of
- java.util package.
- */
- import java.util.Arrays;
- public class SortCharArrayExample {
- public static void main(String[] args) {
- //create char array
- char[] c1 = new char[]{'d','a','f','k','e'};
- //print original char array
- System.out.print("Original Array :\t ");
- for(int index=0; index < c1.length ; index++)
- System.out.print(" " + c1[index]);
- /*
- To sort java char array use Arrays.sort() method of java.util package.
- Sort method sorts char array in ascending order and based on quicksort
- algorithm.
- There are two static sort methods available in java.util.Arrays class
- to sort a char array.
- */
- //To sort full array use sort(char[] c) method.
- Arrays.sort(c1);
- //print sorted char array
- System.out.print("\nSorted char array :\t ");
- for(int index=0; index < c1.length ; index++)
- System.out.print(" " + c1[index]);
- /*
- To sort partial char array use
- sort(char[] c, int startIndex, int endIndex) method where startIndex is
- inclusive and endIndex is exclusive
- */
- char[] c2 = new char[]{'d','a','f','k','e'};
- Arrays.sort(c2,1,4);
- //print sorted char array
- System.out.print("\nPartially Sorted char array :\t ");
- for(int index=0; index < c2.length ; index++)
- System.out.print(" " + c2[index]);
- }
- }
- /*
- Output Would be
- Original Array : d a f k e
- Sorted char array : a d e f k
- Partially Sorted char array : d a f k e
- */
this site is to help students to learn coding in java platform and qbasic platform. all programs are ICSE and ISC level. Java programs java python Python programs
Java Sort char Array Example
Subscribe to:
Post Comments (Atom)
Mersenne Number
Write a program to check if a number is a Mersenne number or not. In mathematics , a Mersenne number is a number that can be written in...
-
import java.util.*; class riwi { public static void main(String[]args) { Scanner sc=new Scanner(System.in); System...
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...
-
Chapter 1 - Unit 9 Nested Loop Class 10 - APC Understanding Computer Applications with BlueJ Write whether the following statements are Tr...
No comments:
Post a Comment
any problem in any program comment:-