1

Hellow

I created a class Packet as followed

public class Packet 
{
    private long arrivalTime = 0;
    private boolean classType = true;

    public Packet(long arrivalTime, boolean classType)
    {
        this.arrivalTime = arrivalTime;
        this.classType = classType;
    }

In a program I have two methods;

public ArrayList<Packet> simulateArrProces (long loopCount)
{
//DO STUFF
}

public void enqueue(Packet...packets)
{
//DO STUFF
}

The objective is to use the ArrayList returned by the simulateArrProces method as an input for the enqueue method. I thought I could simply give the ArrayList of the first method, but this doesn't seem to work. I tried the following

Arrival arr = new Arrival(arrivalProces);
Queue que = new QueueFCFS();
List l = arr.simulateArrProces(0);
que.enqueue(l.toArray());

It is clear that I should tranform the returned ArrayList of simulateArrProces to something, but I don't find what to. I think this is due to the fact that I chose to use ' Packet ... packet ' , but I did not exactly get what this expression meant.

In short; two questions 1. How to transform the ArrayList returned by simulateArrProces to something I can use in the enqueue method. 2. What is the exact meaning of Packet... packets

1
  • Dont return type ArrayList, instead just return the interface List. Commented Feb 11, 2016 at 14:18

2 Answers 2

3

The toArray method of Collection (from which Lists and subsequently ArrayLists inherit) features an overload, taking an (empty) array of T (your parametrized type) as parameter: this is what you should employ in order not to return an Object[].

In turn, this can be passed into a method taking a varargs of that type as parameter.

Example

Given...

List<String> foo() {
    // just as example
    return new ArrayList<String>();
}
void bar(String... s) {
    // nope
}

Here are the "good" vs "bad" idioms available to you:

List<String> foo = foo();
// bad, casting Object[] as String[]
bar((String[])foo.toArray());
// good, populating a String[] of your List's size with your List's elements
bar(foo.toArray(new String[foo.size()]));
Sign up to request clarification or add additional context in comments.

Comments

2

Let's start with Packet...packets. It's var-args feature of Java. This means that you can pass there any number of packet objects. Implicitly it converts to array of Packet so you can pass an array as well

Example 1:

class Packet {
  // your implementation goes here
}

class SomeClassWithVarArgs{
   void foo(Packet ... packet);
}


// So you can do the following:


Packet p1 = new Packet();
Packet p2 = new Packet();
Packet p3 = new Packet();

SomeClassWithVarArgs s = new SomeClassWithVarArgs();

// all these are legal
s.foo(p1, p2,p3);
s.foo(p1, p2);
s.foo(p3, p3,p3,p3,p3);

// implicit conversion to array:
Packet [] arrayOfPackets = new Packet[] {p1, p2,p3};
s.foo(arrayOfPackets);

Now regarding the toArray, it's just a compilation related issue you'll have to learn.

Obviously you can't pass neither ArrayList not List to enqueue(Packet... packets). So you've tried to convert the list to array and it's a good attempt. However toArray returns Object [] and obviously it's not what you want.

So you should use the following approach:

Example 2:

List<Packet> li = ...
Packet [] arr = li.toArray(new Packet[li.size()]);

 // now you can pass it to enqueue method
enqueue(arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.