Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.Arrays;
import java.util.Random;

public class Main {

    public static int[] getRandomArray(int length, int max) {
        int[] randArray = new int[length];
        Random rand = new Random();
        for (int i = 0; i < length; i++) {
            randArray[i] = rand.nextInt(max);
        }
        Arrays.sort(randArray);
        for (int i = 1; i < length; i++) {
            if (randArray[i] == randArray[i - 1]) {
                randArray[i] += 1;
            }
        }
        for (int i = length - 1; i > 0; i--) {
            if (randArray[i] == randArray[i - 1]) {
                randArray[i - 1] -= 1;
            }
        }
        return randArray;

    }
}