containercomparison.java
来自「这是Java思想的最高集萃」· Java 代码 · 共 39 行
JAVA
39 行
package chapter16;
import java.util.*;
class BerylliumSphere{
private static long counter;
private final long id=counter++;
public String toString(){
return "Sphere "+id;
}
}
public class ContainerComparison {
public static void main(String args[]){
BerylliumSphere[] spheres=new BerylliumSphere[10];
/*
* 上面的语句说明Java可以现时实例化多个对象
*/
for(int i=0;i<5;i++){
spheres[i]=new BerylliumSphere();
/*
* 每实例化开一次,c
*
* ounter++运行一次
*/
}
System.out.println(Arrays.toString(spheres));
/*
* Arrays.toString()是java的一个方法,不过,可以自己写toString方法,这样,
* 本程序的结果不是
* [chapter16.BerylliumSphere@1fb8ee3, chapter16.BerylliumSphere@61de33, chapter16.BerylliumSphere@14318bb, chapter16.BerylliumSphere@ca0b6, chapter16.BerylliumSphere@10b30a7, null, null, null, null, null]
chapter16.BerylliumSphere@10b30a7
而是
[Sphere 0, Sphere 1, Sphere 2, Sphere 3, Sphere 4, null, null, null, null, null]
Sphere 4
*/
System.out.println(spheres[4]);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?