如何使用迭代器从Java中的两个类中检索对象

时间:2017-05-11 09:51:25

标签: java arraylist collections iterator

我是Java的新手并且正在学习有关集合的知识。我在使用来自两个类对象的Iterator检索数据时遇到问题,我只能为任何单个类而不是两个类获取输出。

这是我的代码:

package ncl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;


public class Test {

public static void main(String[] args) {

    Market m1 = new Market(1234, "Suryam", 1200.10);
    Market m2 = new Market(4321, "SWathi", 1200.10);
    Market m3 = new Market(1111, "Chinna", 1200.10);
    Market m4 = new Market(2222, "Malli Bava", 1200.10);
    Market m5 = new Market(3333, "Vali", 1200.10);

    RetailShop rt1 = new RetailShop("Swathi", 8121720217l, 500087);
    RetailShop rt2 = new RetailShop("Tumbu", 9989330442l, 500087);
    RetailShop rt3 = new RetailShop("Suri Babu", 9010627821l, 500087);

    ArrayList<Object> al = new ArrayList<Object>();

    // Adding Market
    al.add(m1);
    al.add(m2);
    al.add(m3);
    al.add(m4);
    al.add(m5);

    // Adding RetailShop
    al.add(rt1);
    al.add(rt2);
    al.add(rt3);

    Iterator itr = al.iterator();

    while(itr.hasNext())
    {
        Market mt=(Market)itr.next();

            System.out.println(mt.billamount);
            System.out.println(mt.customer);
            System.out.println(mt.invoiceno);

        RetailShop rts=(RetailShop)itr.next();
        System.out.println(rts.custName);
    }
}
}

有人能告诉我这里做错了什么吗?我想打印Market数据以及RetailShop数据

4 个答案:

答案 0 :(得分:3)

使用instanceof运算符检查列表中的对象类型

Iterator itr = al.iterator();

    while (itr.hasNext()) {
        Object o = itr.next();
        if (o instanceof Market) {
            Market mt = (Market) o;
            System.out.println(mt.billamount);
            System.out.println(mt.customer);
            System.out.println(mt.invoiceno);
        }

        if (o instanceof RetailShop) {
            RetailShop rts = (RetailShop)o;
            System.out.println(rts.custName);
        }
    }

答案 1 :(得分:1)

首先,带有对象的ArrayList是不好的做法,更喜欢使用这个

List<Market> a1 = new ArrayList<>();
List<RetailShop> a2 = new ArrayList<>();

//Adding Market
al.add(m1);
al.add(m2);
al.add(m3);
al.add(m4);
al.add(m5);
//Adding RetailShop

a2.add(rt1);
a2.add(rt2);
a3.add(rt3);

for(Market mt:a1){
   System.out.println(mt.billamount);
   System.out.println(mt.customer);
   System.out.println(mt.invoiceno);
}
for(RetailShop rts:a2){
   System.out.println(rts.custName);
}

如果你想使用一个列表,那么你确实需要强制转换,但是你应该像这样检查一下:

while(itr.hasNext())
{
    Object o = itr.next();
    if(o instanceof Market){
        Market mt = (Market) o;
        System.out.println(mt.billamount);
        System.out.println(mt.customer);
        System.out.println(mt.invoiceno);
    }else if(o instanceof RetailShop){
        RetailShop rts=(RetailShop) o;
        System.out.println(rts.custName);
    }

}

这样做是检查列表中每个元素的类。如果它是一个市场,它会将该对象转移到市场并为其执行相关代码。零售店也来了。

答案 2 :(得分:1)

由于在同一个ArrayList中有不同的对象(Market和RetailShop),因此如果对象是正确的(右)对象,则应首先检查该对象。例如,要将对象转换为市场,您应该确保此对象是市场。

Iterator itr=al.iterator();

while(itr.hasNext())
{
Object obj=itr.next();
if (obj instanceof Market){
    Market mt=(Market)obj;
    System.out.println(mt.billamount);
    System.out.println(mt.customer);
    System.out.println(mt.invoiceno);
    } 
else {
     if(obj instanceof RetailShop)
      {
      RetailShop rts=(RetailShop)obj;
      System.out.println(rts.custName);
      }
     }
}

答案 3 :(得分:0)

由于您的列表持有对象,因此您需要在进行强制转换之前先检查类型

  List<Object> l = Arrays.asList(1, null, 3, "Hello", 5, null);

    Iterator<Object> it = l.iterator();

    while (it.hasNext()) {
        Object x = (Object) it.next();

        if (x != null) {
            System.out.println("getClass: " + x.getClass());
            if ("integer".equalsIgnoreCase(x.getClass().getSimpleName())) {
                System.out.println("This is an int: " + (Integer) x);
            } else if ("string".equalsIgnoreCase(x.getClass().getSimpleName())) {
                System.out.println("This is a string: " + (String) x);
            }

        } else {
            System.out.println("the element is null");
        }
    }