使用交替列表避免超出范围

时间:2013-10-11 18:14:44

标签: java list foreach

这个方法应该包含两个列表,然后将它们放入一个新的列表然后返回它。

例如,对于(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;
}

3 个答案:

答案 0 :(得分:3)

您可以使用并行运行的2 Iterators执行此操作。使用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等。

答案 1 :(得分: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;
}

答案 2 :(得分: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;
}