A more efficient way to code this program
This Program WAS homework. We have already finished it and are good to go. I was wondering if there is a more streamlined way of writing this program? The program is called Interleave and what it does is take two ArrayLists and combine them so that every other element in the first one is from the second ArrayList. Sounds simple, and it was, we used the Iterator to go through and add the necessary elements. But the code is BLOCKY. It seems to me that there has to be a better way to write this, right? Thanks in advance.   


import java.util.*;

public class Interleave
{

	public static void main(String[] args)
	{

		ArrayList<Integer> a1 = new ArrayList<Integer>();
		Collections.addAll(a1, 10, 20, 30);

		ArrayList<Integer> a2 = new ArrayList<Integer>();
		Collections.addAll(a2, 4, 5, 6, 7, 8, 9);

		System.out.println(a1);
		System.out.println(a2);

		System.out.println(interleave(a1, a2));

		ArrayList<String> list = new ArrayList<String>();
		String[] words =
		{ "how", "are", "you?" };
		
		for (String s : words)
		{
			list.add(s);
		}
		
	}

	public static ArrayList<Integer> interleave(ArrayList<Integer> a1,
			ArrayList<Integer> a2)
	{
		Iterator<Integer> it = a2.iterator();
		int i = 1;
		while (it.hasNext())
		{
			int val = it.next();
			if (a1.size() >= i)
			{
				a1.add(i, val);
			} else
			{
				a1.add(val);
			}
			i += 2;
		}
		return a1;

	}
}
A more efficient way to code this program
===

This Program WAS homework. We have already finished it and are good to go. I was wondering if there is a more streamlined way of writing this program? The program is called Interleave and what it does is take two ArrayLists and combine them so that every other element in the first one is from the second ArrayList. Sounds simple, and it was, we used the Iterator to go through and add the necessary elements. But the code is BLOCKY. It seems to me that there has to be a better way to write this, right? Thanks in advance.   


import java.util.*;

public class Interleave
{

	public static void main(String[] args)
	{

		ArrayList<Integer> a1 = new ArrayList<Integer>();
		Collections.addAll(a1, 10, 20, 30);

		ArrayList<Integer> a2 = new ArrayList<Integer>();
		Collections.addAll(a2, 4, 5, 6, 7, 8, 9);

		System.out.println(a1);
		System.out.println(a2);

		System.out.println(interleave(a1, a2));

		ArrayList<String> list = new ArrayList<String>();
		String[] words =
		{ "how", "are", "you?" };
		
		for (String s : words)
		{
			list.add(s);
		}
		
	}

	public static ArrayList<Integer> interleave(ArrayList<Integer> a1,
			ArrayList<Integer> a2)
	{
		Iterator<Integer> it = a2.iterator();
		int i = 1;
		while (it.hasNext())
		{
			int val = it.next();
			if (a1.size() >= i)
			{
				a1.add(i, val);
			} else
			{
				a1.add(val);
			}
			i += 2;
		}
		return a1;

	}
}