2

I want to make a simulation of a queueing system in Java.

public void enqueue(Packet...packets)
{ 
  // here the Packet object needs to be added to an existing ArrayList..

}

I tried to add the Packet as follows in my existing Arraylist queue

queue.add(packets)

but this doesn't work.

How can I best do this ? I can't give an ArrayList as argument, it has to be Packet...packets.

Tom
  • 16,842
  • 17
  • 45
  • 54
StudentX
  • 565
  • 1
  • 4
  • 7

2 Answers2

2

I'm guessing that "but it doesn't work" you mean, you want to add each packet to a separate entry of your array.

you can iterate through your array and add one packet at a time.

public void enqueue(Packet...packets)
{ 
  for(Packet packet : packets){
    queue.add(packet);
  }
}

NOTE:

Packet... packets (more or less) is another way of saying Packet[] packets

nafas
  • 5,283
  • 3
  • 29
  • 57
2

The issue is that Packets... is an array. Please try:

queueList.addAll(Arrays.asList(packets));
shalama
  • 1,133
  • 1
  • 11
  • 25