import java.util.ArrayList;
import java.util.List;
import java.util.Date;
public class ListDemo{
public class Person{
int age;
String name;
Person(){
this.age=24;
this.name="liming";
}
Person(int age){
this.age=age;
this.name="df";
}
public int getAge(){
return this.age;
}
public String getName(){
return this.name;
}
public int setAge(){
return this.age;
}
public String setName(){
return this.name;
}}
public static void main(String [] args){
List<Person> list=new ArrayList<Person>();
list.add(Person());
list.add(Person(5));
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}

解决方案 »

  1.   

    System.out.println(list.get(i));list.get(i) 是取得 person这个对象
    你试可以看看System.out.println(list.get(i).getName());
    System.out.println(list.get(i).getAge());
      

  2.   

    public class ListDemo{
    public class Person{这样写就是内部类了啦  类里面除了内部类不能这样写的 根据你的意图 其实可以把类写在其他的包里面 或者写在同一个包里面  如下::package com.testClass;import java.util.ArrayList;
    import java.util.List;public class ListDemo { public static void main(String[] args) {
    List<Person> list = new ArrayList<Person>();
    list.add(new Person());
    list.add(new Person(5));
    for (int i = 0; i < list.size(); i++) {
    System.out.println(list.get(i));
    }
    }
    }class Person {
    int age;
    String name; Person() {
    this.age = 24;
    this.name = "liming";
    } Person(int age) {
    this.age = age;
    this.name = "df";
    } public int getAge() {
    return this.age;
    } public String getName() {
    return this.name;
    } public int setAge() {
    return this.age;
    } public String setName() {
    return this.name;
    }}
    输出如下 :
    com.testClass.Person@c17164
    com.testClass.Person@1fb8ee3因为你没用重写toString方法 所以打印出来是默认的hash码 你可以重写toString方法 、或者按照一楼所说的 打印具体:System.out.println(list.get(i).getName());
    System.out.println(list.get(i).getAge());
      

  3.   

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Date;
    public class ListDemo{

    public static class Person{    //这里加了Static

    int age;
    String name;

    Person(){
    this.age=24;
    this.name="liming";
    }

    Person(int age){
    this.age=age;
    this.name="df";
    }

    public int getAge(){
    return this.age;
    }

    public String getName(){
    return this.name;
    }

    public int setAge(){
    return this.age;
    }

    public String setName(){
    return this.name;
    }
    }

    public static void main(String [] args){

    List<Person> list=new ArrayList<Person>();
    list.add(new Person()); //这行加了new,Person内部类必须是Static的,静态方法只能调用 静态内部成员
    list.add(new Person(5)); //这行加了new,Person内部类必须是Static的,静态方法只能调用 静态内部成员
    for(int i=0;i<list.size();i++){
    System.out.println(list.get(i));
    }
    }
    }
    楼主看对不对咯
      

  4.   

    输出结果 为:
    Test.ListDemo$Person@4f1d0d
    Test.ListDemo$Person@1fc4bec包名.内名.内部类名@内存地址
      

  5.   

    如果输出结果为楼上所说的,那你可以试试用Array.toString()这个方法试试
      

  6.   

    楼主有什么问题也不直接说啊,一个Java文件中不可能出现2个public类,你这个文件中
    public class ListDemo{
    public class Person{
    肯定是无法编译的啊
      

  7.   

    问题是啥啊
    生成对象要new    直接Person()是啥意思
    直接打印对象会默认调用它的类中的toString()方法
      

  8.   

    我写的这个java程序有问题,希望各位高手帮我看看哪有问题,提示说找不到方法
      

  9.   

    提示说找不到符号list.add(Person(5));
    list.add(person());
      

  10.   

    提示说找不到符号list.add(Person(5));
    list.add(person());