如何在Java中获取子列表的元素

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

How to get elements of a sublist in Java

问题

I want to save all the numbers in the first list: "list".
我想保存在第一个列表中的所有数字:"list"。

If the numbers are divisible by 2, I also want to save them in the second list: "list2".
如果这些数字能被2整除,我也想把它们保存在第二个列表中:"list2"。

My problems are:
我的问题是:

  1. Which parameters should the sublist get? I dont think (list.subList (0, list.size ()) are correct;)
  2. 子列表应该获取哪些参数?我认为(list.subList(0, list.size()))不正确;
  3. How can I get the elements of the sublist "list2" in my print () method?
  4. 我如何在我的print()方法中获取子列表 "list2" 的元素?

Thanks for your help!
谢谢你的帮助!

英文:

I've already googled but couldn't find a satisfactory solution.

I want to save all the numbers in the first list: "list".

If the numbers are divisible by 2, I also want to save them in the second list: "list2"

My problems are:

  1. Which parameters should the sublist get? I dont think (list.subList (0, list.size ()) are correct;)
  2. How can I get the elements of the sublist "list2" in my print () method?

I can only pass the first list as parameter

Thanks for your help!

ArrayList<Integer> list = new ArrayList<Integer>();
	ArrayList<Integer> list2 = new ArrayList<Integer>(list.subList(0, list.size()));

	list.add(2);
	list.add(1);
	list.add(4);
	list.add(5);
	list.add(10);

	for (int i = 0; i < list.size(); i++) {

		if (list.get(i) % 2 == 0) {
			list2.add(list.get(i));
		}
	}

	print(list);

}

private static void print(ArrayList<Integer> list) {

	for (Integer integer : list) {
		System.out.println(integer);
	}

}

答案1

得分: 1

ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();

list.add(2);
list.add(1);
list.add(4);
list.add(5);
list.add(10);
for (int i = 0; i < list.size(); i++) {
    if (list.get(i) % 2 == 0) {
        list2.add(list.get(i));
    }
}

print(list, list2);



private static void print(ArrayList<Integer> list, ArrayList<Integer> list2) {

    for (Integer integer : list) {
        System.out.println(integer);
    }
    System.out.println("----sublist----");
    for (Integer integer : list2) {
        System.out.println(integer);
    }
}

print(list);
print(list2);

private static void print(ArrayList<Integer> list) {

    for (Integer integer : list) {
        System.out.println(integer);
    }

}

print(list);

private static void print(ArrayList<Integer> list) {

    for (Integer integer : list) {
        System.out.println(integer);
        if (integer % 2 == 0) {
            System.out.println("sub list----" + integer);
        }
    }

}
英文:
ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
ArrayList&lt;Integer&gt; list2 = new ArrayList&lt;Integer&gt;();
list.add(2);
list.add(1);
list.add(4);
list.add(5);
list.add(10);
for (int i = 0; i &lt; list.size(); i++) {
if (list.get(i) % 2 == 0) {
list2.add(list.get(i));
}
}
print(list, list2);
private static void print(ArrayList&lt;Integer&gt; list, ArrayList&lt;Integer&gt; list2) {
for (Integer integer : list) {
System.out.println(integer);
}
System.out.println(&quot;----sublist----&quot;);
for (Integer integer : list2) {
System.out.println(integer);
}
}

print(list);
print(list2);
private static void print(ArrayList&lt;Integer&gt; list) {
for (Integer integer : list) {
System.out.println(integer);
}
}

print(list);
private static void print(ArrayList&lt;Integer&gt; list) {
for (Integer integer : list) {
System.out.println(integer);
if(integer%2==0){
System.out.println(&quot;sub list----&quot;+integer);
}
}
}

答案2

得分: 1

声明列表为全局变量。并将第一个列表作为参数传递。在打印方法中打印子列表。

public class Main {

    static ArrayList<Integer> list = new ArrayList<Integer>();
    static ArrayList<Integer> list2 = new ArrayList<Integer>(list.subList(0, list.size()));

    public static void main(String[] args) {

        list.add(2);
        list.add(1);
        list.add(4);
        list.add(5);
        list.add(10);

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) % 2 == 0) {
                list2.add(list.get(i));
            }
        }

        System.out.println("------------原始列表------------");
        print(list);

    }

    private static void print(ArrayList<Integer> list) {
        for (Integer integer : list) {
            System.out.println(integer);
        }

        System.out.println("------------子列表------------");

        for (Integer integer : list2) {
            System.out.println(integer);
        }
    }
}

输出:

------------原始列表------------
2
1
4
5
10
------------子列表------------
2
4
10
英文:

Declare the List globally. And send the first list as the parameter. Print the sublist in the print method.

public class Main {
static ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
static ArrayList&lt;Integer&gt; list2 = new ArrayList&lt;Integer&gt;(list.subList(0, list.size()));
public static void main(String[] args) {
list.add(2);
list.add(1);
list.add(4);
list.add(5);
list.add(10);
for (int i = 0; i &lt; list.size(); i++) {
if (list.get(i) % 2 == 0) {
list2.add(list.get(i));
}
}
System.out.println(&quot;------------------Orginal List------------------&quot;);
print(list);
}
private static void print(ArrayList&lt;Integer&gt; list) {
for (Integer integer : list) {
System.out.println(integer);
}
System.out.println(&quot;------------------Sub List------------------&quot;);
for (Integer integer : list2) {
System.out.println(integer);
}
}

Output :

------------------Original List------------------                                                                                                            
2                                                                                                                                                           
1                                                                                                                                                           
4                                                                                                                                                           
5                                                                                                                                                           
10                                                                                                                                                          
------------------Sub List------------------                                                                                                                
2                                                                                                                                                           
4                                                                                                                                                           
10

答案3

得分: 1

Sure, here's the translated code portion:

可以使用Java的Streams来实现这个它更干净更易于使用而且代码更少

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        List<Integer> list2 = new ArrayList<Integer>();

        list.add(2);
        list.add(1);
        list.add(4);
        list.add(5);
        list.add(10);

        list2 = list.stream()
            .filter(number -> number % 2 == 0)
            .collect(Collectors.toList());

        for (int number : list2) {
            System.out.println(number);
        }
    }
}

Is there anything else you'd like to be translated or help with?

英文:

you can use java Streams to achieve this. It's cleaner and easier to work with, and also less code.

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
List&lt;Integer&gt; list2 = new ArrayList&lt;Integer&gt;();
list.add(2);
list.add(1);
list.add(4);
list.add(5);
list.add(10);
list2 = list.stream()
.filter( number -&gt; number%2==0)
.collect(Collectors.toList());
for(int number : list2) {
System.out.println(number);
}
}
}

答案4

得分: 1

If you change the scope of sub list. So print() can assess.

public class Test {

	static ArrayList<Integer> 子列表 = new ArrayList<Integer>();
	public static void main(String[] args) {

		ArrayList<Integer> 列表 = new ArrayList<Integer>();
		 列表.add(2);
		    列表.add(1);
		    列表.add(4);
		    列表.add(5);
		    列表.add(10);

		    for (int i = 0; i < 列表.size(); i++) {

		        if (列表.get(i) % 2 == 0) {
		        	子列表.add(列表.get(i));
		        }
		    }

		    打印(列表);

		}

		private static void 打印(ArrayList<Integer> 列表) {

		    for (Integer 整数 : 列表) {
		    	if(子列表.contains(整数))
		        System.out.println(整数);
		    }

		}

}
英文:

If you change the scope of sub list. So print() can assess.

public class Test {
static ArrayList&lt;Integer&gt; sublist = new ArrayList&lt;Integer&gt;();
public static void main(String[] args) {
ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
list.add(2);
list.add(1);
list.add(4);
list.add(5);
list.add(10);
for (int i = 0; i &lt; list.size(); i++) {
if (list.get(i) % 2 == 0) {
sublist.add(list.get(i));
}
}
print(list);
}
private static void print(ArrayList&lt;Integer&gt; list) {
for (Integer integer : list) {
if(sublist.contains(integer))
System.out.println(integer);
}
}
}
</details>
# 答案5
**得分**: 0
如果您使用Java 8或更高版本,我建议使用[流API][streamingAPI]:
```java
public static List<Integer> extractEvens(final List<Integer> numbers) {
return numbers.stream()
.filter(value -> value % 2 == 0)
.collect(Collectors.toList());
}

Ideone演示

英文:

If one uses Java 8 or later, I would suggest using the streaming API:

public static List&lt;Integer&gt; extractEvens(final List&lt;Integer&gt; numbers) {
return numbers.stream()
.filter(value -&gt; value % 2 == 0)
.collect(Collectors.toList());
}

<kbd>Ideone demo</kbd>

huangapple
  • 本文由 发表于 2020年8月2日 01:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63207878.html
匿名

发表评论

匿名网友

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

确定