Java:交错两个基于整数的数组列表 ->好方法? [英] Java: Interleave two integer based arraylists -> good approach?
问题描述
家庭作业:寻找更好的策略或方法,而不是完整的代码.
我在两种情况下得到了两个整数数组:
I'v got two arrayLists of integers under two conditions:
- 第一个列表比第二个大
- 第二个列表比第一个大
我的目标是在两种情况下将 list2 的元素交错到 list1 中.我已经创建了一个方法来做到这一点,但我觉得我可以做得更好.
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.
这里是条件 1 的预期结果.注意,在 list2 的元素耗尽后,我们将 list1 的元素留在原地:
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]
这里是条件 2 的预期结果.由于 list2 有更多元素,我们在 list1 用完后将这些元素附加到 list1:
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]
我的代码使用 if-else 语句来处理这两个条件.然后我使用迭代器遍历 list2 的元素并将它们插入到 list1 中.
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());
}
}
}
}
推荐答案
您喜欢这个解决方案吗?
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;
}
这篇关于Java:交错两个基于整数的数组列表 ->好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!