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

  • Home
  • SEARCH
  • 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 9192773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:00:30+00:00 2026-06-17T21:00:30+00:00

Homework: Looking for better strategy, or approach rather than complete code. I’v got two

  • 0

Homework: Looking for better strategy, or approach rather than complete code.

I’v got two arrayLists of integers under two conditions:

  1. the first list is bigger than the second
  2. the second list is larger than the first

My goal was to interleave elements of list2, into list1 under both conditions. I’ve created a method that does this, but I feel like I could be doing something better.

Here is the expected result for condition 1. Note that after the elements of list2 are exhausted, we leave the elements of list1 in place:

list1: [10, 20, 30, 40, 50, 60, 70]
list2: [4, 5, 6, 7]
Combined: [10, 4, 20, 5, 30, 6, 40, 7, 50, 60, 70]

Here is the expected result for condition 2. Since list2 has more elements, we append these elements to list1 after list1 is exhausted:

list1: [10, 20, 30, 40]
list2: [4, 5, 6, 7, 8, 9, 10, 11]
Combined: [10, 4, 20, 5, 30, 6, 40, 7, 8, 9, 10, 11]

My code uses an if-else statement to process both conditions. I then use an iterator to go through elements of list2 and insert them in list1.

public static void main(String[] Args)
{
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    numbers.add(10);
    numbers.add(20);
    numbers.add(30);
    numbers.add(40);
    //numbers.add(50);
    //numbers.add(60);
    //numbers.add(70);

    ArrayList<Integer> numbers2 = new ArrayList<Integer>();

    numbers2.add(4);
    numbers2.add(5);
    numbers2.add(6);
    numbers2.add(7);
    numbers2.add(8);
    numbers2.add(9);
    numbers2.add(10);
    numbers2.add(11);

    System.out.println("list1: " + numbers);
    System.out.println("list2: " + numbers2);

    interleave(numbers, numbers2);

    System.out.println();
    System.out.println("Combined: " + numbers);
}

public static void interleave(ArrayList<Integer> list1, ArrayList<Integer> list2)
{
    //obtain an iterator for the collection
    Iterator<Integer> itr2 = list2.iterator();

    //loop counter
    int count = 1;

    //handle based on initial size of lists
    if(list1.size() >= list2.size())
    {
       //loop through the first array and add elements from list 2 after each element
       while(itr2.hasNext())
       {
           //insert elements from list2
           list1.add(count, itr2.next());

           //make sure elements are getting added at 1, 3, 5, 7, 9, etc
           count = count + 2;
       }
    }
    else if(list1.size() < list2.size())
    {
       //loop through the first array and add elements from list 2 after each element
       while(itr2.hasNext())
       {
           if(count <= list1.size())
           {
               //insert elements from list2
               list1.add(count, itr2.next());

               //make sure elements are getting added at 1, 3, 5, 7, 9, etc
               count = count + 2;
           }
           else
           {
               //fill in the remainder of the elements from list2 to list1
               list1.add(itr2.next());
           }
       }
    }
}
  • 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-17T21:00:31+00:00Added an answer on June 17, 2026 at 9:00 pm

    Do you like this solution?

    public static void main(final String[] args) {
        ArrayList<Integer> numbers = new ArrayList<Integer>();
        numbers.add(10); numbers.add(20); numbers.add(30); numbers.add(40);
        //numbers.add(50); numbers.add(60); numbers.add(70);
    
        ArrayList<Integer> numbers2 = new ArrayList<Integer>();
        numbers2.add(4); numbers2.add(5); numbers2.add(6); numbers2.add(7);
        numbers2.add(8); numbers2.add(9); numbers2.add(10); numbers2.add(11);
    
        System.out.println("list1: " + numbers);
        System.out.println("list2: " + numbers2);
        List<Integer> interleaved = interleave(numbers, numbers2);
    
        System.out.println("\nCombined: " + interleaved);
    }
    
    public static List<Integer> interleave(
        final List<Integer> list1,
        final List<Integer> list2
    ) {
        List<Integer> result
            = new ArrayList<Integer>(list1.size() + list2.size());
    
        Iterator<Integer> it1 = list1.iterator();
        Iterator<Integer> it2 = list2.iterator();
        while (it1.hasNext() || it2.hasNext()) {
            if (it1.hasNext()) {
                result.add(it1.next());
            }
            if (it2.hasNext()) {
                result.add(it2.next());
            }
        }
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for suggestions on how to approach my Perl programming homework assignment to write
I am working on a homework assignment looking at inheritance in java. I am
Homework. Dice Game. I've got an array that represents five rolls of a die.
I'm looking to compare two big sets of csv files and/or a csv file
I want to print a range of numbers to STDOUT. But rather than count
This is a homework. I'm not looking for answers, just a nudge into the
This is a homework question, I got the basics down, but I can't seem
For a homework assignment I wrote some scala code in which I have the
I have a homework assignment. I'm not looking for anyone to do the work
I'm new at Java. I'm looking for some help with homework. I wont post

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.