example8_1.java

来自「《Java程序设计与应用》-张仕斌-源程序 《Java程序设计与应用》-张仕斌」· Java 代码 · 共 40 行

JAVA
40
字号
/*
 * Example8_1.java
 *
 * Created on 2006年9月16日, 上午9:41
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package example8_1;
import java.util.*;

/**
 *
 * @author Administrator
 */
public class Example8_1 {
    
    /** Creates a new instance of Example8_1 */
    public Example8_1() {
    }
	static Random rand=new Random(); //创建并声明了一个Random对象
 	static int pRand(int mod){ return Math.abs(rand.nextInt())%mod+1;}    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        // TODO code application logic here
 	Integer[] a=new Integer[pRand(20)];  //声明一个数组,并为其分配内存空间
        System.out.println("length of a= "+a.length);
        for(int i=0;i<a.length;i++)
        {
            a[i]=new Integer(pRand(500));  //对数组元素进行初始化
            System.out.println("a["+i+"]= "+a[i]);   		
        }
    }
    
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?