Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Copy an Integer arraylist's even numbers to another arraylist

ArrayList<Integer> list3 = new ArrayList<>();       
        for(int i=0;i<=100;i++) {
            list3.add(i);
        }       
        System.out.println(list3);

I created an arraylist like this,its zero to 100 numbers and i want to copy even numbers to another arraylist.How can i do that?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Try the code below:

        ArrayList<Integer> list3 = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list3.add(i);
        }
        ArrayList<Integer> eventIntegerList = new ArrayList<>();
        for (Integer integer : list3) {
            if (integer % 2 == 0)
                eventIntegerList.add(integer);
        }
        System.out.println(eventIntegerList);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading