Question

6:02 PM Wed Jan 29 83% stepik.org stepik 2222222222222222 NH Nathanael UCSD CSE 11 Course progress: 63/92 We have declared aplease answer the question in java. Thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class SortedExample {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       ArrayList<Integer> listOfNums=new ArrayList<>(Arrays.asList(1,3,4,5));
       System.out.println("Before adding");
       System.out.println(listOfNums);
       System.out.println("After addding");
       SortedExample.addSorted(listOfNums,2);
       System.out.println(listOfNums);
   }
   public static void addSorted(ArrayList<Integer> sortedNums,int newNum){
       sortedNums.add(newNum);
       Collections.sort(sortedNums);
   }
}

Expected output:

Before adding [1, 3, 4, 5] After addding [1, 2, 3, 4, 5]

Add a comment
Know the answer?
Add Answer to:
please answer the question in java. Thank you! 6:02 PM Wed Jan 29 83% stepik.org stepik...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers...

    A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers as a a parameter and returns a new ArrayList that has every element in the second half of the original ArrayList swapped with every element in the first half of the original ArrayList. Note: • You can assume that the ArrayList passed to this method as a parameter always contains an even number of elements and will always contain at least two elements. For...

  • Java: please ignore my answer, do question 3. Question 3. 15 pts. The equalsO method of...

    Java: please ignore my answer, do question 3. Question 3. 15 pts. The equalsO method of the ArrayList<E> class takes another list as a parameter and returns true if the two lists contain the same elements in the same order, and false otherwise middle section of the code below so that the output is truetrue . Remember that add (int index, E element), and set (int index, E element). you can put elements into an ArrayList with the methods add(E...

  • Search 12:40 pm Wed 13 Mar C Get Homework Help With c × Th| Assessment-CSSE1001 S...

    Search 12:40 pm Wed 13 Mar C Get Homework Help With c × Th| Assessment-CSSE1001 S × D https://mytut2.uqcloud.net × + 3 a mytut2.uqcloud.net Due: 20:30 15th Mar ﹀ [CSSE1001/7030] Introduction to Software Engineering (St Lucia) Semester 1, 2019-(4/43) completed if sign 2019 Save Attempt Reset Attempt Run Code 2 int string input('7 3 x--int (int_string) Week 2: Introductory Programming - (3/3) completed If Statements Using Elif and Else print( "monty" elif x-7 print( python) elif x 3: print( 'flying')...

  • Please code in Java and ignore question 2, thank you! 1. Write a Java method that...

    Please code in Java and ignore question 2, thank you! 1. Write a Java method that takes as its only input parameter a single integer, n, and returns the number of unique binary search trees that can be constructed with integers 1 through n. (Hint: You'll probably want to do this recursively.) (Note: This is not asking for the number of maximally tall BSTs. The BSTs can be any height.) 2. Work through the deletion exercises in 2-4-deletion-supplementary.pdf (attached above)....

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link cl...

    Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link class by writing methods described below. Do not use loops, or create any more methods (other than those specified), class or instance variables. public class Link private Link next; /null if this is the last link private int value; public Link(Link n, int v) nextn valuev; Do not use loops, or create any more methods (other than those specified), class or...

  • in java no mathcall please ennas are out of date.Please sign was wkollieemail.cpeedu so we can...

    in java no mathcall please ennas are out of date.Please sign was wkollieemail.cpeedu so we can verily your subscription Sign In ASSIGNMENT Unit 9 Methods, Arrays, and the Java Standard Class Library Reimplementing the Arrays class (60 points) The Arrays class, which is also part of the Java standard class library, provides various class methods that perform common tasks on arrays, such as searching and sorting. Write a public class called MyArrays, which provides some of the functionality which is...

  • Please answer this programming question as fast as possible. Will upvote 45 pts Question 2 Let...

    Please answer this programming question as fast as possible. Will upvote 45 pts Question 2 Let BSTree be the following class which implements Binary Search Tree (BST). a. (5 points) Write the method public int maxElement 0 that returns the element of the tree with maximum value if the BST is not empty and returns -1 otherwise. b. (10 points) Write the method public void preOrderPrint 0 that prints out all the elements in the BST in the order of...

  • I only need help with question 5! Thank you! Question 4 4 pts Consider this variant...

    I only need help with question 5! Thank you! Question 4 4 pts Consider this variant of the linear search algorithm which attempts to speed up the search by reducing the number of iterations of the for loop by approximately one-half. During each pass of the loop, the algorithm performs two comparisons rather than one comparison as in linear Search(). The two comparisons compare the elements at indices i and pList size() - 1-i to pKey for equality. If a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT