0

I have a class InQueue with the following:

static Queue<String> q = null;

public InQueue(String[] input){
    q = new LinkedList<String>();

}
public static Queue<String> newInputQueue(String[] inputArray){
    q.addAll(Arrays.asList(inputArray));
    q.add("$");
    return q;
}

And I have an array in main that I'm trying to do this:

String[] inputArr = {"id", "+", "id", "*", "id"};
InQueue inQueue = new InQueue(inputArr);

I want to pass inputArr to inQueue so that my array goes into the queue. But obviously I can't do that because the InQueue constructor doesn't have parameters. Is there a way to do this? I've tried various ways and most of them either don't work, or they return an empty queue.

8
  • "because the InQueue constructor doesn't have parameters" - why are you not creating one? Commented Dec 1, 2021 at 13:04
  • I tried that and I still get an empty queue Commented Dec 1, 2021 at 13:07
  • Please show your attempt and we might find out what the issue is Commented Dec 1, 2021 at 13:09
  • 1
    Are u sure about the static Queue<String> q ? Commented Dec 1, 2021 at 13:10
  • 1
    You never added the values from the string array to q, and q being static means you can only ever have a single instance of q at any given time. If you made a new empty queue after the fact, it'd overwrite/erase any other queues to begin with. That's why you shouldn't use static here, particularly for a (design-wise) instance variable. Commented Dec 1, 2021 at 13:16

3 Answers 3

2

You can simply create that constructor yourself:

public InQueue(String[] arr){
    q = new LinkedList<String>(Arrays.asList(arr));
}

You should also remove the static keyword from both the field q as well as the newInputQueue method, is there a reason for it?

Sign up to request clarification or add additional context in comments.

3 Comments

I've tried this, and it returns an empty queue. I'm not quite sure why as my input array definitely returns the strings on it's own
Please post the full code that "returns an empty queue". This constructor takes the array and initializes the field q with a LinkedList. If the array has elements, so will the queue.
I fixed it. I didn't realize you had to use Arrays.asList, and that's why I was getting an empty queue. Thank you for your help, I will mark this as the correct answer
0

Change your class to something like this:

private Queue<String> q = null;

private InQueue(){
    q = new LinkedList<String>();    
}
public static InQueue newInputQueue(String[] inputArray){
    InQueue res = new InQueue();
    res.q.addAll(Arrays.asList(inputArray));
    res.q.add("$");
    return res;
}

What are the main differences between this and your code ? The first is the lack of the static on the data member. static there means that object is "static" (kind of unique/shared) for all instances of your class. Get rid of it to avoid future headaches.

The constructor has become private, this tells any user, there should be an alternative way to construct the object.

The alternative way to construct the object is the static function, which can invoke the private constructor (because they're in the same classe) and can access any datamember of the class itself.

Now whenever you need to allocate an InQueue, you just use the static factory.

InQueue myQueue = InQueue.newInputQueue(someArray);

Comments

0

Easiest way is to add another constructor to your InQueue class that converts the array to a LinkedList. Something like:

public InQueue(String[] inputArr) {
  q = new LinkedList<> (Arrays.asList(inputArr));
}

Alternatively, you can use the method you already have and reassign like:

String[] inputArr = {"id", "+", "id", "*", "id"};
InQueue inQueue = new InQueue();
inQueue = inQueue.newInputQueue(inputArr);

1 Comment

That didn't work for me because I can't put type String[] in InQueue

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.