Java interview questions, java questions

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.