BeginnersBook

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Sorting float array in Java example

By Chaitanya Singh | Filed Under: Java Tutorials

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
    Arrays.sort(floatArray);
    System.out.println("**Float Array After Sorting**");
    for (float temp: floatArray){
       System.out.println(temp);
    }
 
    // Another Char Array
    float[] floatArray2 = 
       new float[] { 1.9f, 19.9f, 9.8f, 27.5f, 2.1f, 3.5f, 1.1f};
 
    // Selective Sorting
    /* public static void sort(float[] a, int fromIndex,
     * int toIndex): Sorts the specified range of the 
     * array into ascending order. The range to be sorted 
     * extends from the index fromIndex, inclusive, to the 
     * index toIndex, exclusive. If fromIndex == toIndex, 
     * the range to be sorted is empty.
     */
    Arrays.sort(floatArray2, 2, 5);

    // Displaying array after selective sorting 
    System.out.println("**Selective Sorting**");
    for (float temp: floatArray2){
       System.out.println(temp);
    }
  }
}

Output:

**Float Array Before Sorting**
21.1
9.9
9.8
7.5
2.1
**Float Array After Sorting**
2.1
7.5
9.8
9.9
21.1
**Selective Sorting**
1.9
19.9
2.1
9.8
27.5
3.5
1.1

References:
sort(float[] array) method – javadoc
sort(float[] array, int fromIndex, int toIndex) method – javadoc

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Control Statements

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor
  • Java Interview Q

MORE ...

  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java String
  • Java Date
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap