英文:
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<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;
}
}
}
答案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<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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论