Java interview questions, java questions
1/** 2 * write a function. It accepts an integer (assuming length) and returns an array. The length of the array is length, 3 * the content in the array is a random value ranging from 0 to (length-1) and cannot be repeated. 4*5 * If the length is 5, the array may be [, 4]. 6*7 * @ author Administrator 8*9 */10 11 import java. util. arrayList; 12 import java. util. list; 13 import java. util. random; 14 15 public class RandomArray {16 public static void main (String [] args) {17 int [] a = randomArray (5); 18 for (int B:) {19 System. out. println (B); 20} 21} 22 23 public static int [] randomArray (int n) {24 Random ran = new Random (); 25 int [] a = new int [n]; 26 List <Integer> value S = new ArrayList <Integer> (); 27 int index = 0; 28 while (true) {29 int temp = ran. nextInt (n); 30 if (! Values. contains (temp) {31 values. add (temp); 32 a [index ++] = temp; 33} 34 if (index = n) 35 break; 36} 37 return a; 38} 39} View Code