Related questions
Given an ArrayList called myAL that contains the following:
["blue", "red", "yellow"]
write code to insert the elements "purple" and "orange" that keeps the list in alphabetical order.
Algorithm:
1) First we add the elements into the array list
2) then print the array list
3) then add remaining elements into the array list
4) sort the array list
5)print the sorted arraylist
Code: in java
import java.io.*;
import java.util.ArrayList;
import java.util.*;
class Arraylist1 //class declaration
{
public static void main(String[] args) //main method
throws IOException
{
int size = 5;
ArrayList<String> myAL = new ArrayList<String>(size);
myAL.add("blue");//adding the elements into the list
myAL.add("red");
myAL.add("yellow");
System.out.println(myAL);//printing the added elements
myAL.add("purple");//adding remaining elements into the arraylist
myAL.add("orange");
Collections.sort(myAL);//sorting the array list
System.out.println(myAL);//printing the array list elements after sorting
for (int i=0; i<myAL.size(); i++)
System.out.print(myAL.get(i)+" ");//printing the final output
}
}
Code Screenshot:
Computer Science homework question answer, step 1, image 1
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 2 images
- Java Complete the method with explaining, thank you!arrow_forwardWrite a declaration for a one dimensional array, called List holding letters of the alphabet.arrow_forwardJAVA Chapter 9 Multidimensional Arrays and the ArrayList Class Write a value returning method that returns the sum of the elements in the last column of each row in a two-dimensional array of ints. Include code to test your method.arrow_forward
- Create a class: "Question 1" with data members: 1D integer array of maximum size: 100, n(int). Create a dynamic constructor which takes input of n and n no. of array elements. Apartfrom taking input this class also displays the maximum and minimum elements from the givenarray elements.arrow_forwardComplete the method endsInFive that takes as arguments a 2D-array of integers and an integer value that represents a column index in the array. endsInFive returns the number of values in the column that end in 5. It is possible that the array is ragged, meaning that the number of columns in each row is different. For example, 2 is returned if the following 2D-array is passed as the first argument of endsInFive and the integer 3 as the second argument (the values 25 and 115 end in five at column index 3).arrow_forwardModify the quick sort implementation in the textbook to sort the array using pivot as the median of the first, last, and middle elements of the array. Add the modified quick sort implementation to the arrayListType class provided (arrayListType.h). Ask the user to enter a list of positive integers ending with -999, sort the integers, and display the pivots for each iteration and the sorted array. Main Function #include <iostream>#include "arrayListType.h"using namespace std; int main(){ arrayListType<int> list;int num;cout << "Line 8: Enter numbers ending with -999" << endl;cin >> num; while (num != -999){list.insert(num);cin >> num;} cout << "Line 15: The list before sorting:" << endl;list.print();cout << endl; list.selectionSort();cout << "Line 19: The list after sorting:" << endl;list.print();cout << endl; return 0;} Header File (arrayList.h) Including images #include <iostream>#include <cassert>...arrow_forward
- How do you do this? JAVAarrow_forwardWrite the following method that returns the sum of all numbersin an ArrayList:public static double sum(ArrayList<Double> list)Write a test program that prompts the user to enter five numbers, stores them inan array list, and displays their sum.arrow_forwardGiven an array arr of type int, along with two int variables i and j, write some code that swaps the values of arr[i] and arr[j]. Declare any additional variables as necessary.arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education