public class Test3{
public static void main(String[] args) {
ArrayList<Sample> l = new ArrayList<Sample>();
Test3.Sample s1= new Test3().new Sample("zhangsan");//
Test3.Sample s2= new Test3().new Sample("lisi");//
l.add(s1);
l.add(s2);
class Sample {
private String name;
// private int age;
public Sample(){
}
public Sample(String name){
this.name=name;
//this.age=age;
}
public String getName() {
return name;
}
}
}
<4>第二种修改方法,直接把Sample类写到Test3类的外部,并把类名''public class Sample"应该改为"class Sample",代码如下:
import java.util.ArrayList;
import java.util.Iterator;
//import java.util.List;
public class Test3{
public static void main(String[] args) {
ArrayList<Sample> l = new ArrayList<Sample>();
Sample s1= new Sample("zhangsan");//
Sample s2= new Sample("lisi");//
l.add(s1);
l.add(s2);
class Sample {
private String name;
// private int age;
public Sample(){
}
public Sample(String name){
this.name=name;
//this.age=age;
}
public String getName() {
return name;
}
}
作者: 38323243 时间: 2013-1-27 20:32
import java.util.*;//一句就够了
public class text
{
public static void main(String[] args)
{
ArrayList l = new ArrayList();
l.add(new Sample("zhangsan",20));
l.add(new Sample("lisi",22));
Iterator it = l.iterator();
while(it.hasNext())
{
Sample sa = (Sample)it.next();
System.out.println(sa.getName()+":"+sa.getAge());
}
}
}
class Sample
{
private String name;
private int age;
Sample(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public int getAge()
{