如何将两个数组交错地合并成一个新数组。

huangapple go评论80阅读模式
英文:

How to interleave two arrays into a new array

问题

/**
 * 创建一个新的列表,其中包含list1和list2的元素交错存放。
 * 例如,如果list1包含<"over","river","through","woods">,
 * 而list2包含<"the","and","the">,
 * 那么新列表应包含
 * <"over","the","river","and","through","the","woods">。
 * 在list1和list2之间交替放置。如果其中一个列表更长,新列表将在末尾包含较长列表的所有额外值。
 * 例如,如果list1包含<"over","river","through","woods">,
 * 而list2包含<"the","and">,
 * 那么新列表应包含
 * <"over","the","river","and","through","woods">。
 */

private static List<String> mergeLists(List<String> list1, List<String> list2) {
    int max = Math.max(list1.size(), list2.size());
    ArrayList<String> newlist = new ArrayList<String>();
    for (int i = 0; i < max; i++) {
        if (i < list1.size()) {
            newlist.add(list1.get(i));
        }
        if (i < list2.size()) {
            newlist.add(list2.get(i));
        }
    }
    return newlist;
}

注意:在代码中我进行了一些修正,以便使其正确工作。首先,我修正了获取列表大小的方法,使用了.size()而不是类型不匹配的.length()。其次,我将append改为了add,因为ArrayList使用的是add方法来添加元素。最后,我进行了语法修正,确保代码逻辑正确。

英文:
  • Creates a new List that holds the elements of list1 interleaved
  • with the elements of list2. For example, if list1 holds
  • <"over","river","through","woods"> and list2 holds <"the","and","the">,
  • then the new list should hold
  • <"over","the","river","and","through","the","woods">. Alternating between
  • list1 and list2. If one list is longer, the new list will contain all of
  • the extra values from the longer list at the end. For example, if list1
  • holds <"over","river","through","woods"> and list2 holds <"the","and">
  • then the new list should hold
  • <"over","the","river","and","through","woods">.

I suck at programing and can't see the logic on the last part of this assignment. Thank you for taking the time to look at this.
//*

private static List&lt;String&gt; mergeLists(List&lt;String&gt; list1, List&lt;String&gt; list2) {
	long max = Math.max(((File) list1).length(),((File) list2).length());
	ArrayList&lt;String&gt; newlist = new ArrayList&lt;String&gt;();
	for (int i = 0; i &lt; max; i++) {
		if (i &lt; list1) {
			newlist.append(list1[i]);
			{
		if (i &lt; list2) {
			newlist.append(list2[i]);
		}
	}


			return newlist; 
		}
	}
}

答案1

得分: 1

你的想法基本正确,你差不多理解对了。看来你在编程方面还不错 : )。你可以使用List的属性来实现这个,而不需要转换为File

    public static void main(String[] args) {
        List<String> list1 = new ArrayList<>();
        list1.add("over");
        list1.add("river");
        list1.add("through");
        list1.add("woods");

        List<String> list2 = new ArrayList<>();
        list2.add("the");
        list2.add("and");

        mergeLists(list1, list2);
    }

    private static List<String> mergeLists(List<String> list1, List<String> list2) {

        // 获取两个列表的最大长度
        int max = Math.max(list1.size(), list2.size());

        // 初始化新列表
        List<String> newList = new ArrayList<>();

        // 将第一个列表的元素添加到新列表中(如果有更多元素)
        // 然后将第二个列表的元素添加到新列表中(如果有更多元素)
        // 依此类推...
        for (int i = 0; i < max; i++) {
            if (i < list1.size()) {
                newList.add(list1.get(i));
            }

            if (i < list2.size()) {
                newList.add(list2.get(i));
            }
        }

        System.out.println(newList);
        return newList;
    }
英文:

You definitely had the right idea, you almost got it. Guess you don't suck at programming that much :). You can use properties of a List for this, without casting to a File.

    public static void main(String[] args) {
        List&lt;String&gt; list1 = new ArrayList&lt;&gt;();
        list1.add(&quot;over&quot;);
        list1.add(&quot;river&quot;);
        list1.add(&quot;through&quot;);
        list1.add(&quot;woods&quot;);

        List&lt;String&gt; list2 = new ArrayList&lt;&gt;();
        list2.add(&quot;the&quot;);
        list2.add(&quot;and&quot;);

        mergeLists(list1, list2);
    }

    private static List&lt;String&gt; mergeLists(List&lt;String&gt; list1, List&lt;String&gt; list2) {

        // Get the max length of both arrays
        int max = Math.max(list1.size(), list2.size());

        // Initialize new list
        List&lt;String&gt; newList = new ArrayList&lt;&gt;();

        // add an element of the first list to the new list (if there are more elements)
        // and then add an element from the second list to the new list (if there are more elements)
        // and repeat...
        for (int i = 0; i &lt; max; i++) {
            if (i &lt; list1.size()) {
                newList.add(list1.get(i));
            }

            if (i &lt; list2.size()) {
                newList.add(list2.get(i));
            }
        }

        System.out.println(newList);
        return newList;
    }

huangapple
  • 本文由 发表于 2020年4月8日 05:14:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/61089513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定