使用迭代器从List中获取数据

时间:2015-04-24 14:54:39

标签: java arraylist

我正在尝试使用List获取数据,但使用迭代器。它正在显示我的对象。如何从中获取数据?

import java.util.*;
    class Getter_Setter
    {
        int id;
        String name;
        public List<Getter_Setter> buckets;
        public String getName()
        {
            return name;
        }
        public int getId()
        {
            return id;
        }
        public void setId(int id)
        {
            this.id=id;
        }
        public void setName(String name)
        {
            this.name=name;
        }
        public void setGetter_Setter(List<Getter_Setter> buck)
        {
            this.buckets=buck;
        }
    }
    class Simple
    {
        public static void main(String arg[])
        {
            run();
        }
        public static void run()
        {

            List<Getter_Setter> getList=sum();
            Iterator itr=getList.iterator();
            while(itr.hasNext())
            {
                System.out.println(itr.next());
            }
        }
        public static List<Getter_Setter> sum()
        {
            List<Getter_Setter> list=new ArrayList<Getter_Setter>();
            Getter_Setter get=new Getter_Setter();
            get.setId(30);
            get.setName("Hanish");

            System.out.println(get.getId());
            System.out.println(get.getName());
            list.add(get);
            return  list;
        }
    }

3 个答案:

答案 0 :(得分:1)

您的sum方法返回一个类型为List<Getter_Setter>的方法,因此您可以从中获取强类型迭代器:

List<Getter_Setter> getList = sum();
Iterator itr<Getter_Setter> = getList.iterator();

允许您直接检索Getter_Setter个元素,您可以在其中调用getId()getName()方法:

Iterator<Getter_Setter> itr = getList.iterator();
while (itr.hasNext()) {
    Getter_Setter item = its.next();
    System.out.println(item.getId());
    System.out.println(item.getName());
}

答案 1 :(得分:0)

使用foreach样式进行迭代。它是一种通用的类型迭代方法。它会自动将对象变为您的通用类型。即obj到Getter_Setter的实例。

public static void run() {

        List < Getter_Setter > getList = sum();


       foreach(Getter_Setter g: getList){

          System.out.println(g.getId());

         System.out.println(g.getName());
          } 
    }

答案 2 :(得分:0)

由于您没有覆盖任何toString()方法,因此它会为您提供对象哈希码 您需要覆盖此(结束代码段)以打印您要从该类打印的内容,否则

toString()类的默认Object实现,即:

  public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
println()方法

中传递对象时会调用

 class Getter_Setter {
        int id;
        String name;
        public List < Getter_Setter > buckets;
        public String getName() {
            return name;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public void setName(String name) {
            this.name = name;
        }
        public void setGetter_Setter(List < Getter_Setter > buck) {
            this.buckets = buck;
        }
        @Override
        public String toString() {
            return "Getter_Setter [id=" + id + ", name=" + name + ", buckets="
                    + buckets + "]";
        }