1

该方法应该接受两个列表,然后将它们放入一个新列表中,然后返回该列表。

例如,对于 (1, 2, 3, 4, 5) 的第一个列表和 (6, 7, 8, 9, 10, 11, 12) 的第二个列表,alternate(list1, list2) 的调用应该返回一个包含 (1, 6, 2, 7, 3, 8, 4, 9, 5, 10, 11, 12) 的列表。

我有它的工作,但它看起来一团糟,有没有办法简化事情?

public static List<Integer> alternate(List<Integer> list1, List<Integer> list2) {
List<Integer> template = new LinkedList<Integer>();

int i = 0; // counts where list1 goes into new list
int j = 1; // counts where list2 goes into new list

    if (list1.size() != 0) { // If first list isn't empty
    for (Integer elem1: list1) { // copies all of first list into new list.
        template.add(i, elem1);
        i++;
    }

    for(Integer elem2: list2) { // copies every element into new list into every othe element
        if (j < template.size()) { // if j is not at point where first list ends.
            template.add(j, elem2);
            j+=2;
            } else {
            template.add(j, elem2); // if j is at point where first list ends.
                j++;
            }
        }
    } else {
        for (Integer elem1: list2) { // If first list is empty
            template.add(i, elem1);
            i++;
        }
    }

    return template;
}
4

3 回答 3

3

您可以使用 2 个并行运行的迭代器来执行此操作。使用List#iterator()方法获取列表的迭代器。

public static List<Integer> alternate(List<Integer> list1, List<Integer> list2) {
    Iterator<Integer> iter1 = list1.iterator();
    Iterator<Integer> iter2 = list2.iterator();
    List<Integer> merged = new ArrayList<>();

    // Iterate while there is element in any of the list
    while (iter1.hasNext() || iter2.hasNext()) {
        if (iter1.hasNext()) {
            // This will stop adding once the iter1 has exhausted
            merged.add(iter1.next());
        }
        if (iter2.hasNext()) {
            // This will stop adding once the iter2 has exhausted
            merged.add(iter2.next());
        }
    }

    return merged;
}

public static void main(String[] args) {
    List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
    List<Integer> list2 = Arrays.asList(6, 7, 8, 9, 10, 11, 12);

    System.out.println(alternate(list1, list2));
}

您甚至可以使用通用方法,该方法适用于任何类型的列表:

public static <T> List<T> alternate(List<T> list1, List<T> list2) {
    Iterator<T> iter1 = list1.iterator();
    Iterator<T> iter2 = list2.iterator();
    List<T> merged = new ArrayList<>();

    while (iter1.hasNext() || iter2.hasNext()) {
        if (iter1.hasNext()) {
            merged.add(iter1.next());
        }
        if (iter2.hasNext()) {
            merged.add(iter2.next());
        }
    }

    return merged;
}

这将能够合并List<String>-s,List<Double>s等。

于 2013-10-11T18:19:21.267 回答
2

自然。Rohit 展示了一个使用迭代器的解决方案,这是一个使用手动迭代的解决方案:

public List<Integer> alternate(List<Integer> list1, List<Integer> list2) {
  List<Integer> list3 = new ArrayList<Integer>();

  List<Integer> shortest, longest;
  if (list1.size() < list2.size()) {
    shortest = list1;
    longest = list2;
  } else {
    shortest = list2;
    longest = list1;
  }

  // do paired insertion for as many elements as are 'shared'
  int i, last;
  for(i=0, last = shortest.size(); i < last; i++) {
    list3.add(list1.get(i));
    list3.add(list2.get(i));
  }

  // them simply pad the list with the runoff only found in the longest list
  last = longest.size();
  for(; i<last; i++) {
    list3.add(longest.get(i));
  }

  return list3;
}
于 2013-10-11T18:19:14.910 回答
1

尝试使用迭代器,同时从两个列表中添加元素。

public static List<Integer> alternate(List<Integer> list1, List<Integer> list2) {
    List<Integer> template = new LinkedList<>();

    // Add elements alternately from each of the input lists.
    Iterator<Integer> it1 = list1.iterator();
    Iterator<Integer> it2 = list2.iterator();

    while (it1.hasNext() && it2.hasNext()) {
        template.add(it1.next());
        template.add(it2.next());
    }

    // Add the extra elements from whichever list has not reached the end.
    while (it1.hasNext()) {
        template.add(it1.next());
    }
    while (it2.hasNext()) {
        template.add(it2.next());
    }

    return template;
}
于 2013-10-11T18:23:23.417 回答