In: Computer Science
JAVA PROGRAMMING
1)BuildLists:
Write a program to create an ArrayList<Integer>.
Fill it with numbers from 1 to 1000.
Then remove every even number.
Then remove every multiple of 3 remaining
Then remove every multiple of 5
Then remove every multiple of 7
Then sum the array, and print.
import java.util.*;
class Main{
public static void main(String args[])
{
List<Integer> numbers=new ArrayList<Integer>();
for(int i=1;i<=1000;i++)
{
numbers.add(i);
}
for(Iterator<Integer>
iterator=numbers.iterator();iterator.hasNext();)
{
Integer number=iterator.next();
if(number%2==0)
{
iterator.remove();
}
}
for(Iterator<Integer>
iterator=numbers.iterator();iterator.hasNext();)
{
Integer number=iterator.next();
if(number%3==0)
{
iterator.remove();
}
}
for(Iterator<Integer>
iterator=numbers.iterator();iterator.hasNext();)
{
Integer number=iterator.next();
if(number%5==0)
{
iterator.remove();
}
}
for(Iterator<Integer>
iterator=numbers.iterator();iterator.hasNext();)
{
Integer number=iterator.next();
if(number%7==0)
{
iterator.remove();
}
}
int sum=0;
for(Iterator<Integer>
iterator=numbers.iterator();iterator.hasNext();)
{
Integer number=iterator.next();
sum=sum+number;
}
System.out.println(numbers);
System.out.println(sum);
}
}