Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8866035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:40:39+00:00 2026-06-14T16:40:39+00:00

I have my own ArrayQueue Class which has methods for enqueue , dequeue and

  • 0

I have my own ArrayQueue Class which has methods for enqueue, dequeue and peek etc. My ArrayQmerge class extends this Class and contains:

    public void mergeQs(ArrayQmerge q){

    }

I was wondering whats the best way to write a method to merge an ArrayQueue with another Queue without removing any elements from the q thats passed.

eg. queue1 = [1,2,3,4,11] and queue2 = [5,6,7,8,9,10,12].
When queue1.mergeQs(queue2) is called it will create queue1 = [1,5,2,6,3,7,4,8,11,9,10,12] whilst queue2 would remain [5,6,7,8,9,10,12]

I want the mergeQs method to interleave the elements from the two queues if that makes sense. I know how to successfully add them together or using a method which passes both Queues as arguments but in this case i want to use the above method(which only passes one)…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:40:41+00:00Added an answer on June 14, 2026 at 4:40 pm

    This is what you can do:

    public static void main(String[] args) {
        ArrayQmerge q1 = new ArrayQmerge();
        q1.enqueue(1);
        q1.enqueue(2);
        q1.enqueue(3);
        q1.enqueue(4);
        q1.enqueue(11);
        ArrayQmerge q2 = new ArrayQmerge();
        q2.enqueue(5);
        q2.enqueue(6);
        q2.enqueue(7);
        q2.enqueue(8);
        q2.enqueue(9);
        q2.enqueue(10);
        q2.enqueue(12);
        System.out.println(q1);
        System.out.println(q2);
        q1.mergeQs(q2);
        System.out.println(q1);
        System.out.println(q2);
    }
    
    static class ArrayQmerge{
        private List<Integer> queue = new ArrayList<Integer>();
    
        public void enqueue(int val){
            queue.add(val);
        }
    
        public String toString(){
            return queue.toString();
        }
    
        public void mergeQs(ArrayQmerge q){
            List<Integer> mergedQ = new ArrayList<Integer>(this.queue.size() + q.queue.size());
            for (int i = 0, j = 0; i < this.queue.size() || j < q.queue.size();){
                if (i < this.queue.size())
                    mergedQ.add(this.queue.get(i++));
                if (j < q.queue.size())
                    mergedQ.add(q.queue.get(j++));
            }
            this.queue = mergedQ;
       }
    }
    

    Here is output:

    [1, 2, 3, 4, 11]
    [5, 6, 7, 8, 9, 10, 12]
    [1, 5, 2, 6, 3, 7, 4, 8, 11, 9, 10, 12]
    [5, 6, 7, 8, 9, 10, 12]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have own class which is extends from UITableViewController. After this line MyClass *obj
I have my own generic class which goes like this: C1<T> { T value;
I have own project and i would like add for this class same as
I'm trying to hide window after its startup. I have own window-class which is
I have my own edit in place function for jquery which looks like this
I have my own object class which I use as the data source for
I have my own User Class, which inherits FOS\UserBundle\Entity\User . Additionally I wrote my
I have my own class Event that has few variables like subject and start
I have own component which works in my testing winform app good but when
I have my own installer program which I use to install several applications I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.