Stack Overflow Asked by alvira on February 13, 2021
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:
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);
}
}
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);
}
}
}
Answered by code goku on February 13, 2021
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());
}
Answered by Turing85 on February 13, 2021
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);
}
}
}
Answered by Shihara Dilshan on February 13, 2021
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
Answered by Som on February 13, 2021
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);
}
}
}
Answered by learner on February 13, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP