英文:
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:
我的问题是:
- Which parameters should the sublist get? I dont think (list.subList (0, list.size ()) are correct;)
- 子列表应该获取哪些参数?我认为(list.subList(0, list.size()))不正确;
- How can I get the elements of the sublist "list2" in my print () method?
- 我如何在我的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:
- Which parameters should the sublist get? I dont think (list.subList (0, list.size ()) are correct;)
- 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<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);
}
}
}
答案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<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("------------------Orginal List------------------");
print(list);
}
private static void print(ArrayList<Integer> list) {
for (Integer integer : list) {
System.out.println(integer);
}
System.out.println("------------------Sub List------------------");
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<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);
}
}
}
答案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<Integer> sublist = new ArrayList<Integer>();
public static void main(String[] args) {
ArrayList<Integer> list = 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) {
sublist.add(list.get(i));
}
}
print(list);
}
private static void print(ArrayList<Integer> 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());
}
英文:
If one uses Java 8 or later, I would suggest using the streaming API:
public static List<Integer> extractEvens(final List<Integer> numbers) {
return numbers.stream()
.filter(value -> value % 2 == 0)
.collect(Collectors.toList());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论