Java:按插入顺序复制ArrayList中的项目

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

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&lt;Integer&gt; nums = new ArrayList&lt;&gt;();
          nums.add(1);
          nums.add(5);
          nums.add(3);
          nums.add(7);

          twoTimes(nums);
     }
    
     public static ArrayList&lt;Integer&gt; twoTimes(ArrayList nums) {
          ArrayList&lt;Integer&gt; newNems = new ArrayList&lt;&gt;(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&lt;Integer&gt; twoTimes(ArrayList&lt;Integer&gt; nums) {
	ArrayList&lt;Integer&gt; newNems = new ArrayList&lt;&gt;();

	for(int i = 0; i &lt; 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&lt;Integer&gt; duplicateElements(List&lt;Integer&gt; input) {
    return input.stream()
                .flatMap(i -&gt; 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&lt;Integer&gt; multiplyElements(List&lt;Integer&gt; input, int num) {
    return input.stream()
                .flatMap(i -&gt; IntStream.range(0, num).mapToObj(n -&gt; i)) // getting stream of multiple elements
                .collect(Collectors.toList());
}

huangapple
  • 本文由 发表于 2020年10月19日 05:05:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64418326.html
匿名

发表评论

匿名网友

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

确定