THIS IS JAVA ArrayList and Loops Create a class named Loops and do the following
ID: 3771121 • Letter: T
Question
THIS IS JAVA
ArrayList and Loops Create a class named Loops and do the following:
Create an ArrayList named Contacts and fill with the following Names: John, Maria, Matt, Chris, Jen (in this order).
Write three methods to process the ArrayList using each of the three loop types: for, foreach and while. The methods should be written so that they will still accurately process the ArrayList even after items are added or removed. You will need to create and populate the ArrayList before using the methods to process it. Running any of the loop methods will produce a listing of the names from the ArrayList.
Explanation / Answer
import java.util.ArrayList;
import java.util.Arrays;
class Loops
{
public static void loopfor(ArrayList al)
{
for(int i=0;i<al.size();i++)
{
System.out.println(al.get
(i));
}
}
public static void loopwhile(ArrayList al)
{
int i=0;
while(i<al.size())
{
System.out.println(al.get
(i));
i++;
}
}
public static void loopforeach
(ArrayList<Integer> al)
{
for (int i : al)
{
System.out.println(al.get
(i));
}
}
public static void main(String args[])
{
ArrayList<Integer> aa=new
ArrayList<Integer>();
aa.add(1);
aa.add(2);
aa.add(3);
aa.add(4);
aa.add(5);
loopfor(aa);
loopwhile(aa);
loopforeach(aa);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.