如何将数组列表中的整数进行分割?

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

How to split the integers in the array list?

问题

首先,抱歉英文表达不佳。在我的数组列表中,是否有可能将数字一分为二?我使用 .add 方法添加了每个数字。

public static void main(String[] args) {
    ArrayList<Integer> num = new ArrayList<>();
    num.add(24);
    num.add(18);
    num.add(12);
    num.add(8);
    num.add(20);
    System.out.println(num);
}

输出结果:[24, 18, 12, 8, 20]

我想要将其一分为二,就像这样:

[12, 12, 9, 9, 6, 6, 4, 4, 10, 10]
英文:

first of all sorry for bad english.
Is it possible to split the numbers into half in my array list ?
I added each number using .add

public static void main(String[] args) {
    ArrayList&lt;Integer&gt; num = new ArrayList&lt;&gt;();
    num.add(24);
    num.add(18);
    num.add(12);
    num.add(8);
    num.add(20);
    System.out.println(num);
  
    
}          

Output: [24,18,12,8,20]

I want to split it into half
like this:
[12,12,9,9,6,6,4,4,10,10]

答案1

得分: 3

保持简单:

List<Integer> numSplit = new ArrayList<>();
for (Integer n : num) {
    numSplit.add(n / 2);
    numSplit.add(n / 2);
}
英文:

Keeping it simple:

List&lt;Integer&gt; numSplit = new ArrayList&lt;&gt;():
for (Integer n : num) {
    numSplit.add(n / 2);
    numSplit.add(n / 2);
}

答案2

得分: 3

你还可以使用Java StreamflatMap方法:

List<Integer> num = Arrays.asList(24, 18, 12, 8, 20);
List<Integer> splitted = num.stream()
    .flatMap(n -> Stream.of(n/2, n/2))
    .collect(Collectors.toList());
英文:

You can also use a Java Stream with the flatMap method:

List&lt;Integer&gt; num = Arrays.asList(24, 18, 12, 8, 20);
List&lt;Integer&gt; splitted = num.stream()
    .flatMap(n -&gt; Stream.of(n/2, n/2))
    .collect(Collectors.toList());

答案3

得分: 0

这是一种最简单的方法,没有考虑奇数。

public static void main(String[] args) {
    ArrayList<Integer> num = new ArrayList<>();
    num.add(24);
    num.add(18);
    num.add(12);
    num.add(8);
    num.add(20);
    System.out.println(doJob(num));     
}   

private static List<Integer> doJob(List<Integer> lst){
    List<Integer> retval = new ArrayList<>();
    for(Integer a : lst){
        for(int i = 0; i < 2; i++){
            retval.add(a/2);
        }
    }
    return retval;
}
英文:

This is a simplest approach without any consideration about odd numbers.

public static void main(String[] args) {
    ArrayList&lt;Integer&gt; num = new ArrayList&lt;&gt;();
    num.add(24);
    num.add(18);
    num.add(12);
    num.add(8);
    num.add(20);
    System.out.println(doJob(num));     
}   

private static List&lt;Integer&gt; doJob(List&lt;Integer&gt; lst){
    List&lt;Integer&gt; retval = new ArrayList&lt;&gt;();
    for(Integer a : lst){
        for(int i = 0; i &lt; 2; i++){
            retval.add(a/2);
        }
    }
  return retval;

} 

答案4

得分: 0

有另一种时髦的基于流的拆分方法,其中乘法和除法被位移操作 <<>> 取代:

List<Integer> half2 = IntStream.range(0, num.size() << 1) // 从 0 到 2*n - 1 创建索引
                              .map(i -> num.get(i >> 1) >> 1) // 获取数组中“新”索引对应的相同元素,并将其除以 2
                              .boxed()
                              .collect(Collectors.toList());
英文:

There's another fancy stream-based way of splitting with multiplication and division replaced with shifts &lt;&lt; and &gt;&gt;:

List&lt;Integer&gt; half2 =IntStream.range(0, num.size() &lt;&lt; 1) // create indexes from 0 to 2*n - 1
                              .map(i -&gt; num.get(i&gt;&gt;1)&gt;&gt;1) // get the same element of array for &quot;new&quot; pair of indexes and divide it by 2
                              .boxed()
                              .collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年9月29日 00:07:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64105770.html
匿名

发表评论

匿名网友

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

确定