我很讨厌编程,看不到本作业最后一部分的逻辑。感谢您抽出宝贵的时间对此进行研究。// *
private static List<String> mergeLists(List<String> list1, List<String> list2) {
long max = Math.max(((File) list1).length(),((File) list2).length());
ArrayList<String> newlist = new ArrayList<String>();
for (int i = 0; i < max; i++) {
if (i < list1) {
newlist.append(list1[i]);
{
if (i < list2) {
newlist.append(list2[i]);
}
}
return newlist;
}
}
}
您肯定有正确的想法,您几乎明白了。猜猜你不会那么讨厌编程:)。您可以为此使用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) {
// Get the max length of both arrays
int max = Math.max(list1.size(), list2.size());
// Initialize new list
List<String> newList = new ArrayList<>();
// 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 < 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;
}