Question

In: Computer Science

Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");           &

Java:

Determine the ouotput of this code

ArrayList<String>list=new ArrayList<String>();

            list.add("Namath");

            list.add("Sauer");

            list.add("Maynard");

            list.add("Namath");

            list.add("Boozer");

            list.add("Snell");

            list.add("Namath");

            list.add("Atkinson");

            list.add("Lammonds");

            list.add("Dockery");

            list.add("Darnold");

            list.remove(2);

            list.set(2, "Brady");

            list.remove(2);

            list.set(4,"Unitas");

            list.add(1,"Lamomica");

            list.add(3,"Hanratty");

            list.remove("Namath");

            list.remove(list.size()-1);

            list.remove(2);

            list.set(7, "Riggins");

            Iterator iter = list.iterator();  

        while (iter.hasNext())

        {   System.out.print(iter.next() + " ");  

             

        }

             

      }

}

Solutions

Expert Solution

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

//TestCode.java
public class TestCode {
    public static void main(String[] args) {
        ArrayList<String> list=new ArrayList<String>();

        list.add("Namath");

        list.add("Sauer");

        list.add("Maynard");

        list.add("Namath");

        list.add("Boozer");

        list.add("Snell");

        list.add("Namath");

        list.add("Atkinson");

        list.add("Lammonds");

        list.add("Dockery");

        list.add("Darnold");

        list.remove(2);

        list.set(2, "Brady");

        list.remove(2);

        list.set(4,"Unitas");

        list.add(1,"Lamomica");

        list.add(3,"Hanratty");

        list.remove("Namath");

        list.remove(list.size()-1);

        list.remove(2);

        list.set(7, "Riggins");

        Iterator iter = list.iterator();

        while (iter.hasNext())

        {   System.out.print(iter.next() + " ");
        
        }
    }

}

Lamomica Sauer Boozer Snell Unitas Atkinson Lammonds Riggins


Related Solutions

Determine the outputs of the code: String[]array= {"Namath","Sauer","Namath","Namath","Snell","Snell","Namath","Namath","Namath", "Maynard","Namath","Namath"};        ArrayList<String>list=new ArrayList<String>(); &nbs
Determine the outputs of the code: String[]array= {"Namath","Sauer","Namath","Namath","Snell","Snell","Namath","Namath","Namath", "Maynard","Namath","Namath"};        ArrayList<String>list=new ArrayList<String>();        for(int i=0;i<array.length;i++)        {            list.add(array[i]);        }        System.out.println("OUTPUT 1");        System.out.println(list);        System.out.println("OUTPUT 2");        for(int i=0;i<list.size();i++)        {            if(list.get(i).equals("Namath"))            {                System.out.println(i+"\t"+ list.remove(i));            }        }        System.out.println("OUTPUT 3");        for(String s:list)        {           ...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list =...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list = arrayList; return list; } public static void printList(ArrayList<String> arrayList) { System.out.println("Printing in 4 ways\n"); // 1 System.out.println(arrayList); //2 for(String s:arrayList) System.out.print(s+" "); System.out.println(); //3 System.out.println(Arrays.deepToString(list.toArray())); //4 for(int i=0;i<arrayList.size();i++) System.out.print(arrayList.get(i)+" "); System.out.println(); } public static void filterList(ArrayList<String> arrayList) { System.out.println("Filtered in 2 ways\n"); ArrayList<String> copyArrayList = arrayList; //1 for(int i=0;i<arrayList.size();i++) { if(arrayList.get(i).contains("chuck")) { arrayList.remove(i); i--; } } System.out.println(arrayList); //2 copyArrayList.removeIf(str -> str.contains("chunk")); System.out.println(copyArrayList); }   ...
Move all zeros in an arraylist to the end of the list using a temp(java). if...
Move all zeros in an arraylist to the end of the list using a temp(java). if it is an array the code would look like the code below, change it to fit an arraylist int count = 0;     int temp;     for (int i = 0; i < n; i++) {     if ((arr[i] != 0)) {         temp = arr[count];         arr[count] = arr[i];         arr[i] = temp;         count = count + 1;     }     } }
Complete the code below by finishing up the tasks detailed in the comments. ArrayList<String> fruits =...
Complete the code below by finishing up the tasks detailed in the comments. ArrayList<String> fruits = new ArrayList<>(); Scanner sc = new Scanner(System.in); String fruit = "?"; while (!fruit.isEmpty()) { // prompt the user to enter a fruit // read the fruit using "sc", saving it into variable "fruit" // convert the fruit entered to lowercase // if the fruit is NOT empty and it is a new fruit, add it to ArrayList "fruits" } // display all fruits
3.1 Write code that creates an ArrayList object named list and fills list with these numbers...
3.1 Write code that creates an ArrayList object named list and fills list with these numbers (using one or a pair of for or while loops): 0 1 2 3 4 0 1 2 3 4 3.2 Consider the ArrayList object named list containing these Integers: list = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 } What are the contents of list after this loop completes? for (int i = 1; i < 10; ++i) {...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of the characters in each ArrayList.  For example, if list1 has strings (“cat, “dog”, “boat”, “elephant”) and list 2 has strings (“bat”, “mat”, “port”, “stigma”), you will return the value 18.  The list 1 has 18 characters in total for all its strings combined and list2 has 16 characters for all of its strings combined.  The higher value is 18. If the character count is the same, you...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String...
Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String and returns a copy of that ArrayList with no duplicates. The relative ordering of elements in the new ArrayList should be the same. Sample Input: {"qwerty", "asdfgh", "qwer", "123", "qwerty", "123", "zxcvbn", "asdfgh"} Sample Output: {"qwerty", "asdfgh", "qwer", "123", "zxcvbn"}
since firstN is a string it can't compare to an ArrayList * list so how am i suppose to write these parts comparing the string to the list[mid]
c++ question:int ArrayList::binarySearchID(string firstN) const{   int first = 0;         int last = length - 1;         int mid = (first + last) / 2;   bool found = false;   int index;   while (!found && last >= first)   {       if (firstN == list[mid])       {           found = true;       }       else if (firstN > list[mid])       {           first = mid - 1;       }       else if (firstN < list[mid])       {           last...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT