Question

In: Computer Science

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)
       {
           System.out.println(s);
       }
      
  


/*
OUTPUT 1

OUTPUT 2

OUTPUT 3

*/

Solutions

Expert Solution

ANSWER: Here I am giving you the code and output. The explanation is given in the code as a comment.

CODE:


import java.util.*;
public class Main
{
   public static void main(String[] args) {
       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");// In output here its simply printing the list items
System.out.println(list);
System.out.println("OUTPUT 2");// In output2 if the list items is equal to the Namath then it is removing it from the list and printing at which index it is available.
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"); // In this OUTPUT3 remaining list items will be printed after removing Namath.
for(String s:list)
{
System.out.println(s);
}
   }
}

OUTPUT:


Related Solutions

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() + " ");                         }                     } }
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); }   ...
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
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) {...
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...
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
I need the code for a C++ program that creates an array of 5000 String objects...
I need the code for a C++ program that creates an array of 5000 String objects that will store each word from a text file. The program will read in each word from a file, and store the first 5000 words in the array. The text file should be read in from the command line.
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Modify this linked list code to work with string. Insert the following items into the list...
Modify this linked list code to work with string. Insert the following items into the list and display the list. The items are: Pepsi, Coke, DrPepper, Sprite, Fanta. Insert them in that order. Display the list. Then delete DrPepper and redisplay the list. Then insert 7-UP and redisplay the list. Then append Water and redisplay the list. c++ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ #include <iostream> using namespace std; class ListNode { public:     int value;     ListNode *next;     ListNode(int nodeValue) {       value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT