Page 753
Suppose you're writing a method and you don't want to return just one thing, but a whole bunch of things. Languages like C and C++ make this difficult because you can't just return an array, only a pointer to an array. This introduces problems because it
becomes messy to control the lifetime of the array, which leads to memory leaks.
In Java, you just return the array. You never worry about responsibility for that array -- it will be around as long as you need it, and the garbage collector will clean it up when you're done.
import java.util.Random;
public class Test {
private static Random rand = new Random(47);
static final String[] FLAVORS = {
"Chocolate",
"Strawberry",
"Vanilla Fudge Swirl",
"Mint Chip",
"Mocha Almond Fudge",
"Rum Raisin",
"Praline Cream",
"Mud Pie"
};
private static String[] test(int n) {
if (n > FLAVORS.length) {
throw new IllegalArgumentException("Set too big");
}
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;
}
public static void main(String[] args) {
int yy = 8;
int kk = 10; // times
for (int i = 0; i < kk; i++) {
String[] x = test(yy);
System.out.print("[");
for (int j = 0; j < yy; j++) {
System.out.print(x[j]);
if (j != yy - 1) System.out.print(",");
}
System.out.println("]");
}
}
}
分享到:
相关推荐
Arrays in Java .............................. 44 You never need to destroy an object .................. 45 Scoping ........................................ 45 Scope of objects ...........................
1 Introduction to Java's Architecture Why Java? The Architecture The Java Virtual Machine The Class Loader Architecture The Java Class File The Java API The Java Programming Language ...
的JavaBook :hot_beverage:接口中的静态方法和默认方法(Java +8) 您无法覆盖接口的静态方法; 您只需使用接口名称即可访问它们。 如果尝试通过在实现接口中定义类似的方法来覆盖接口的静态方法,则它将被视为该类...
attr_copy: copy an attribute list, returning the size in bytes.
NULL 博文链接:https://futeng.iteye.com/blog/1833449
_.where(list, properties) Looks through each value in the list, returning an array of all the values that contain all of the key-value pairs listed in _.findWhere(list, properties) Looks through the ...
RETURNING 自己通常结合DML 语句使用。(INSERT UPDATE DELETE) 使用方法: UPDATE table_name SET expr1 RETURNING column_name INTO xxx INSERT: 返回的是添加后的值 UPDATE:返回时更新后的值 DELETE:返回删除...
./include/nasmlib.h:194:1: error: ‘pure’ attribute on function returning ‘void’ [-Werror=attributes] 194 | void pure_func seg_init(void); 这是由于gcc版本过新,解决方案如下 vim ./inclu
An Eco-tour of returning to nature.zip
: That means that drawLine () finally calls a GDI system function on Windows or X11 function on a X11/UNIX machine even if the drawing is done in an off-screen image using the class java.awt.Image....
downloading and unbundling pop31_1_1.zip, add pop3.jar to your CLASSPATH, too. After installing JavaMail 1.1.3, install the JavaBeans Activation Framework. Installing the JavaBeans Activation ...
Returning Values from C to Python 363 The LAME Project 364 The LAME Extension Module 368 Using Python Objects from C Code 380 Summary 383 Exercises 383 Chapter 18: Writing Shareware and ...
本文内容包括以下三个部分:1.如何理解群落构建;2.如何理解beta-NTI;3.如何利用R语言并行计算beta-NTI。初学者可能对群落构建和beta-NTI的含义理解很模糊,我将结合自己的理解对群落构建和beta-NTI进行简要的介绍...
在本文中,我们介绍了Spring Boot AOP中的@AfterReturning注解,它用于定义在目标方法成功返回后执行的切面逻辑。我们通过一个图书管理系统的实例演示了@AfterReturning的使用方式,并在成功查询图书时记录了查询的...
1、/var/log/message显示 Dec 17 13:59:26 localhost kernel: PAL Shim AllocRequest returning NULL Dec 17 13:59:26 localhost kernel: PAL Shim AllocRequest returning NULL Dec 17 13:59:26 localhost ...
新例程STM32F10xFWLib_on_EKSTM32F,内含大量的例程,ADC,DMA,SPI,USART。。。。。。。希望对你有帮助
python库。 资源全名:django_pg_returning-1.0.0-py2.py3-none-any.whl
The usual implementation of malloc and free are unforgiving to errors in their callers' code, including cases where the programmer overflows an array, forgets to free memory, or frees a memory block ...
基于JAVA + MySQL的图书管理系统+WORD文档报告。图书管理系统 Java + MySQL 完整代码,MVC三层架构组织,包含所有用到的图片资源以及数据库文件。 摘 要 本文介绍了图书管理系统开发的整个过程,该系统通过使用...
1 介绍(INTRODUCTION) 3 1.1 为什么要有编码规范(WHY HAVE CODE CONVENTIONS) 3 1.2 版权声明(ACKNOWLEDGMENTS) 3 2 文件名(FILE NAMES) 4 2.1 文件后缀(FILE SUFFIXES) 4 2.2 常用文件名(COMMON FILE NAMES) 4 3 ...