Sort array java

Sort array java

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

Below is example of array of 10 elements:

Sort array java

Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the preceding illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.

The following sort array java program, ArraySortExample, creates an array of integers, puts some values in the array, and sort its value in ascending and descending orders and prints its value:


package com.arraysort;

import java.util.Arrays;
import java.util.Random;

public class ArraySortExample {

	public static void main(String[] args) {

		System.out.println("Example 1:");
		int[] array = new int[10];
		Random rand = new Random();
		for (int i = 0; i < array.length; i++){
			 array[i] = rand.nextInt(100) + 1;
		System.out.println("In reverse order");
		for (int i = array.length - 1; i >= 0; i--)
		    System.out.print(array[i] + " ");

		System.out.println("\nExample 2:");
		int[] array2 = {2,5,3,4,9,8,10};
		System.out.println("In reverse order");
		for (int i = array2.length - 1; i >= 0; i--)
		    System.out.print(array2[i] + " ");




Sort array java


Leave a Reply

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

I am not Robot *