返回数组

1.返回一个数组

package sj.ThinkInJava.Chapter16.Section03.example;

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

/**
 * 返回String数组
 * 
 * @author Timmy
 * @time 2014-07-18
 * 
 */
public class Ice_Cream {

	public static void main(String[] args) {
		for (int i = 0; i < 7; i++)
			System.out.println(Arrays.toString(flavor_Set(3)));
	}

	// 创建随机数对象rand
	private static Random rand = new Random(47);

	// 聚集初始化数组FLAVORS
	static final String[] FLAVORS = { "Chocolate", "Strawberry",
			"Vanilla Fudge Swirl", "Mint Chip", "Mocha Almond Fudge",
			"Rum Raisin", "Praline Cresm", "Mud Pie" };

	/**
	 * 创建容量为n的String型数组results
	 * 
	 * @param n
	 *            =>参数决定数组的容量
	 * @return=>返回一个数组
	 */
	public static String[] flavor_Set(int n) {
		// 超过数组长度,抛出异常
		if (n > FLAVORS.length)
			throw new IllegalArgumentException("数组越界!");

		// 创建数组对象results
		String[] results = new String[n];
		boolean[] picked = new boolean[FLAVORS.length];

		for (int i = 0; i < n; i++) {
			int t;
			do
				t = rand.nextInt(FLAVORS.length);
			while (picked[t]);
			results[i] = FLAVORS[t];
			picked[t] = true;
		}
		return results;
	}
} /*
   * [Rum Raisin, Mint Chip, Mocha Almond Fudge]
   * [Chocolate, Strawberry, Mocha Almond Fudge]
   * [Strawberry, Mint Chip, Mocha Almond Fudge] 
   * [Rum Raisin, Vanilla Fudge Swirl, Mud Pie]
   * [Vanilla Fudge Swirl, Chocolate, Mocha Almond Fudge]
   * [Praline Cresm, Strawberry, Mocha Almond Fudge]
   * [Mocha Almond Fudge, Strawberry, Mint Chip]
   */

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值