英文:
Java: duplicate ArrayList items in order of insertion
问题
import java.lang.reflect.Array;
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<>();
nums.add(1);
nums.add(5);
nums.add(3);
nums.add(7);
twoTimes(nums);
}
public static ArrayList<Integer> twoTimes(ArrayList<Integer> nums) {
ArrayList<Integer> newNums = new ArrayList<>(nums);
newNums.addAll(nums);
System.out.println(newNums);
return newNums;
}
}
英文:
Have two methods, main
and twoTimes
that takes in a single parameter (ArrayList) but returns the said ArrayList doubled and in order.
I have twoTimes repeating the variables within the parameters but it's coming out (1,5,3,7,1,5,3,7) instead of (1,1,5,5,3,3,7,7).
import java.lang.reflect.Array;
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<>();
nums.add(1);
nums.add(5);
nums.add(3);
nums.add(7);
twoTimes(nums);
}
public static ArrayList<Integer> twoTimes(ArrayList nums) {
ArrayList<Integer> newNems = new ArrayList<>(nums);
newNems.addAll(nums);
System.out.println(newNems);
return newNems;
}
}
答案1
得分: 1
你可以迭代遍历“nums”,并将每个项添加两次到新的列表中:
public static ArrayList<Integer> twoTimes(ArrayList<Integer> nums) {
ArrayList<Integer> newNems = new ArrayList<>();
for (int i = 0; i < nums.size(); i++) {
int num = nums.get(i);
newNems.add(num);
newNems.add(num);
}
System.out.println(newNems);
return newNems;
}
英文:
You can iterate over nums
and add each item twice to the new list:
public static ArrayList<Integer> twoTimes(ArrayList<Integer> nums) {
ArrayList<Integer> newNems = new ArrayList<>();
for(int i = 0; i < nums.size(); i++) {
int num = nums.get(i);
newNems.add(num);
newNems.add(num);
}
System.out.println(newNems);
return newNems;
}
答案2
得分: 1
你可以使用流(Stream) API 的 flatMap
方法来在输入列表中复制每个元素:
public static List<Integer> duplicateElements(List<Integer> input) {
return input.stream()
.flatMap(i -> Stream.of(i, i)) // 获取包含重复元素的流
.collect(Collectors.toList());
}
简单测试:
System.out.println(duplicateElements(Arrays.asList(1, 3, 5, 7)));
输出:
[1, 1, 3, 3, 5, 5, 7, 7]
更通用的方法,可以生成每个元素的 num
个副本:
public static List<Integer> multiplyElements(List<Integer> input, int num) {
return input.stream()
.flatMap(i -> IntStream.range(0, num).mapToObj(n -> i)) // 获取包含多个元素的流
.collect(Collectors.toList());
}
英文:
You could be using Stream API flatMap
to duplicate each element in the input list:
public static List<Integer> duplicateElements(List<Integer> input) {
return input.stream()
.flatMap(i -> Stream.of(i, i)) // getting stream of duplicate elements
.collect(Collectors.toList());
}
Simple test:
System.out.println(duplicateElements(Arrays.asList(1, 3, 5, 7)));
Output:
[1, 1, 3, 3, 5, 5, 7, 7]
More general method generating num
copies of each element:
public static List<Integer> multiplyElements(List<Integer> input, int num) {
return input.stream()
.flatMap(i -> IntStream.range(0, num).mapToObj(n -> i)) // getting stream of multiple elements
.collect(Collectors.toList());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论