Question

In: Computer Science

In Java, you can iterate an ArrayList in different ways. Write the following methods to print...

In Java, you can iterate an ArrayList in different ways. Write the following methods to print Integers in an ArrayList iterating in different ways:

1. // Using basic while / for loop
void printArrayListBasicLoop(ArrayList<Integer> al);

2. // Using enhanced for loop (:)
void printArrayListEnhancedLoop(ArrayList<Integer> al);

3. // Using basic for loop with iterator
void printArrayListForLoopListIterator(ArrayList<Integer> al);

4. // Using basic while loop with iterator

void printArrayListWhileLoopListIterator(ArrayList<Integer>

al);

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class LoopArrayLists {

    // Using basic while / for loop
    void printArrayListBasicLoop(ArrayList<Integer> al) {
        for (int i = 0; i < al.size(); i++) {
            System.out.println(al.get(i));
        }
    }

    // Using enhanced for loop (:)
    void printArrayListEnhancedLoop(ArrayList<Integer> al) {
        for (Integer num : al) {
            System.out.println(num);
        }
    }

    // Using basic for loop with iterator
    void printArrayListForLoopListIterator(ArrayList<Integer> al) {
        for (Iterator<Integer> it = al.iterator(); it.hasNext(); ) {
            System.out.println(it.next());
        }
    }

    // Using basic while loop with iterator
    void printArrayListWhileLoopListIterator(ArrayList<Integer> al) {
        Iterator<Integer> it = al.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }

    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(3, 8, 1, 9));
        new LoopArrayLists().printArrayListBasicLoop(list);
        System.out.println();
        new LoopArrayLists().printArrayListEnhancedLoop(list);
        System.out.println();
        new LoopArrayLists().printArrayListForLoopListIterator(list);
        System.out.println();
        new LoopArrayLists().printArrayListWhileLoopListIterator(list);
        System.out.println();
    }
}

Related Solutions

A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings...
A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings representing the names of staff members. The constructor takes an ArrayList of String names as a parameter. In addition to the constructor, you need to implement the following methods The methods 1. public boolean equals(Staff other) - Determines if the other Staff contains all the same elements in the same order as this Staff 2. public boolean sameContents(Staff other) - Determines if the other...
Write a JAVA program that contains the following 4 void methods whic will print all subtrings...
Write a JAVA program that contains the following 4 void methods whic will print all subtrings of s in specific orders: printSub1(String s), printSub2(String s), printSub3(String s), and printSub4(String s) For example, if S = "abcd": printSub1 will print "a", "ab", "abc", "abcd", "b", "bc", "bcd", "c", "cd", "d" printSub2 will print "a", "ab", "b", "abc", "bc", "c", "abcd", "bcd", "cd", "d" printSub3 will print "d", "c", "b", "a", "cd", "bc", "ab", "bcd", "abc", "abcd" printSub4 will print "abcd", "bcd",...
JAVA In this PoD you will use an ArrayList to store different pet names (there are...
JAVA In this PoD you will use an ArrayList to store different pet names (there are no repeats in this list). This PoD can be done in your demo program (where your main method is) – you don’t have to create a separate class for today. Details Create an arraylist of Strings, then using a Scanner object you will first read in a number that will tell you how many pet names (one word) you will add to the arraylist....
In Java, Using ArrayList and HashSet data structures, as well as their methods, obtain following from...
In Java, Using ArrayList and HashSet data structures, as well as their methods, obtain following from some input text file: Total number of characters used,without counting spaces and punctuation, total number of words used; Number of words, counting each word only once; Total number of punctuation characters;Number of words that are of size six or more;Number of words that are used only once
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java...
1. a) Write two algorithms of different time complexity implemented in Java methods in complete Java program to reverse a stack of characters. Make your own assumption and your own design on the methods signature, inputs, outputs, and the full main program.
Write the following methods in a Java project: a) A Java method to determine and return...
Write the following methods in a Java project: a) A Java method to determine and return the sum of first three numbers, where three numbers are received as parameters. b) A Java method to determine and return the highest of N integers. The number of integers is received as a parameter. The method should prompt the user to enter the N numbers, then it return the highest. c) A Java method to determine and return an appropriate value indicating if...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date object, a ClockWithAudio object, a BMI object, a Day object, and a FigurePane object. Then display all elements in the list. Assume that all classes (i.e. Date, Account, etc.) have their own no-argument constructor.
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
using java LO: (Apply) Students will write loops that iterate until a condition is met. LO:...
using java LO: (Apply) Students will write loops that iterate until a condition is met. LO: (Analyze) Students will identify edge cases from a problem statement. In this game of volleyball, two teams compete to be the first team to score 21 points. However, to win the game, a team must also lead by two points. For example, if team A is tied with team B (20-20) and then scores, they will only be ahead by one point (21-20). Then,...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and a method to get all the values of the queue. (This is not writing a file implementing the java class PriorityQueue, but rather you are writing a program that is a priority queue).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT