Java - 另一种方法中的ArrayList为空

时间:2015-04-27 19:40:40

标签: java arraylist

我正在从另一个类的PacketList Class中向ArrayList添加数据包。

public class PacketList {

    public ArrayList<PcapPacket> packets;

    public PacketList(){
        packets = new ArrayList<PcapPacket>();
    }

    public void addPacket(PcapPacket packet){
        packets.add(packet);
        System.out.printf("List size: %d\n", packets.size()); // Size is according to added objects into the list.
    }

到目前为止一切正常,但是当我创建另一个方法来检索列表时,它是空的..

public ArrayList getList(){
    System.out.printf("Size of list in PacketList: %d\n", packets.size()); // Size is 0
    return packets;
}

编辑抱歉,这实际上是整个代码,只是切碎了..

import java.util.ArrayList;
import org.jnetpcap.packet.PcapPacket;  

public class PacketList {

    public ArrayList<PcapPacket> packets;

    public PacketList(){
        packets = new ArrayList<PcapPacket>();
    }

    public void addPacket(PcapPacket packet){
        System.out.printf("Received packet .. caplen=%-4d\n", packet.getCaptureHeader().caplen());
        packets.add(packet);
        System.out.printf("List size: %d\n", packets.size()); // Size is according to added objects into the list.
    }

    public ArrayList getList(){
        System.out.printf("Size of list in PacketList: %d\n", packets.size()); // Size is 0
        return packets;
    }
}

这里我创建了PacketList

public class ThreadToSwitch implements Runnable {

    PacketList pl = new PacketList();

    public void run(){ 

// here I have just pcap init that i did not include, but the packet is ok

PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {  

            public void nextPacket(PcapPacket packet, String user) {  

                if(packet != null)
                    pl.addPacket(packet);
            }  
        };
}

这是我调用getList()的主要内容:

public class SwrMain extends Thread{  

    public static void main(String[] args) {  

        Thread tts = new Thread(new ThreadToSwitch());
        Thread ttr = new Thread(new ThreadToRouter());
        tts.start();
        ttr.start();

        ArrayList pList;
        int myint = -1;
        while(myint != 0){
            Scanner keyboard = new Scanner(System.in);
            myint = keyboard.nextInt();
            if(myint == 1){
                    System.out.printf("\nNumber of packets in list: %d\n", (new PacketList().getList().size()));

            }
        }
    }  
}  

2 个答案:

答案 0 :(得分:0)

这只是一个猜测,但您可能从另一个线程调用PacketList.getList()而不是添加数据包的线程。您可能遇到了一些同步问题。如果是这种情况,请尝试切换到并发结构,可能是ConcurrentLinkedQueue

查看完整代码(包括您阅读列表的代码)有助于诊断问题。

答案 1 :(得分:0)

所以你的问题是你正在创建一个新的数据包列表,然后询问它的大小。它与您在ThreadToSwitch方法中创建的数据包列表不同,但是它自己的实例,在您调用新的PacketList时由PacketList的默认构造函数创建

public class SwrMain extends Thread{  

    public static void main(String[] args) {  

        Thread tts = new Thread(new ThreadToSwitch());
        Thread ttr = new Thread(new ThreadToRouter());
        tts.start();
        ttr.start();

        ArrayList pList;
        int myint = -1;
        while(myint != 0){
            Scanner keyboard = new Scanner(System.in);
            myint = keyboard.nextInt();
            if(myint == 1){
                    System.out.printf("\nNumber of packets in list: %d\n", (new PacketList().getList().size()));

            }
        }
    }  
}  

这样想。我有一只名叫Fido的狗。我喂Fido直到他真的很胖。你还有一只名叫Fido的狗。无论我喂多少Fido,你的Fido都不会发胖。它们是不同的,即使它们被命名为相同的东西。

现在,如果我要借你的Fido一段时间并喂他很多,那么你的Fido会很胖。