class NoteBook{
private ArrayList<String> notes=new ArrayList<String>;
public void add(String s){
notes.add(s);
}
public void add(String s,int index){//多态
notes.add(index,s);
}
public int Getsize(){
return notes.size();
}
}
class Value{
private int i;
public void set(int i){
this.i=i;
}
public int GetValue(){
return i;
}
}
//对象数组的用法————与int[] 不同的是
//对象数组是申请了10个“指针”,作为对象的管理者,
//使用的时候必须new +对象名(相当于malloc)
Value[] a=new Value[10];
for(int i=0;i<a.length;i++){
a[i]=new Value();
a[i].set(i);
}
for(Value i:a){
System.out.println(i.GetValue());
}
for(int i=0;i<a.length;i++){//输出的类似于地址
System.out.println(a[i]);
}
int[] a=new a[10];//创建了10个int类型的空间
JAVA的顺序容器(类似于动态数组与C++中的vector<>)与对象数组
最新推荐文章于 2025-08-04 11:53:30 发布
这篇博客探讨了Java中的类NoteBook如何使用ArrayList添加元素,并展示了对象数组的创建和使用方法。通过示例代码,解释了对象数组不同于基本类型数组的地方,包括如何初始化并设置对象数组中每个元素的值,以及如何遍历输出对象数组的内容。同时,文章还展示了int类型的数组创建过程。
774

被折叠的 条评论
为什么被折叠?



