📄 containercomparison.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -