Write a method that given a non-empty ArrayList of Integers parameterand also an Integer parameter, the method returns a new ArrayList containing the elements from the original ArrayList that come after the first occurrence of Integer parameter in the original ArrayList.
If the original ArrayList does not contain an occurrence of the Integer parameter return an empty ArrayList . The original ArrayList must be unaffected by the method execution.
Following are some examples of the invocation of the method using explicit parameters, with an ArrayList represented by a bracketed list. The returned ArrayList follows the '→' character.
postList({1, 2, 4, 1}, 4); → {1} postList({3, 1, 4}, 3); → {1, 4} postList({1, 4, 4}, 4); → {4} postList({1, 2, 4, 1}, 5); → {}
import java.util.ArrayList;
import java.util.List;
public class ArrayListEx {
public static void main(String[] args) {
ArrayList<Integer> list = new
ArrayList<>();
list.add(3);
list.add(1);
list.add(4);
list.add(5);
System.out.println(postList(list, 3));
}
private static List
postList(ArrayList<Integer> l, int n) {
ArrayList<Integer> list = new
ArrayList<>();
boolean flag = false;
for (int x : l) {
if (x == n)
{
flag = true;
continue;
}
if (flag)
list.add(x);
}
return list;
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me
Write a method that given a non-empty ArrayList of Integers parameterand also an Integer parameter, the...
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...
Write a method that takes an ArrayList of Integer objects and returns an ArrayList of Character objects of the same size. The returned elements of the ArrayList are assigned letter grade corresponding to integer grade of the same index element of the ArrayList parameter (A if 90 or above, F if less than 60). Include code to test your method. [For other letter grades: 80 ?? 89 −> ?, 70 ?? 79 −> ?, 60 ?? 69 −> ?]
//Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a test program that does the following operations: 4. Creates an empty ArrayList of Integer elements; 5. Generates 20 integer values, drawn at random between 0 and 9 (included), and adds them to the array list; 6. Calls the method sort and prints both the original list, and the sorted one.
Write a method maxOccurrences that accepts a list of integers as a parameter and returns the number of times the most frequently occurring integer (the “mode”) occurs in the list. Solve this problem using a map as auxiliary storage. If the list is empty, return 0.
iii.print out the arraylist iv.reverse all the elements v.print out this arraylist vi.make a clone of the arraylist vii.remove all the elements at any odd index of the original arraylist (not the cloned one) viii.print out the original arraylist ix.reverse the cloned arraylist x.print out the cloned arraylist (this arraylist should still contain the original sequence of elements in order) xi.merge the cloned arraylist to the original arraylist (please think about what happens and draw a diagram for yourself to...
Write a method called reverseFirstK that accepts an integer k and a queue of integers as parameters and reverses the order of the first k elements of the queue, leaving the other elements in the same relative order. For example, if a queue named q stores [10, 20 30, 40, 50, 60, 70, 80, 90], the call of reverseFirstK (4, q):should change the queue to store [40, 30 20, 10, 50, 60, 70, 80, 90]. If k is 0 or...
I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...
Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...
Add the following methods to the ArrayList class that we wrote during lecture. You may call the existing methods in ArrayList if you want, but do not use anything from the built-in java.util.ArrayList class. 1. (3 pts) Write a new method named addAll(ArrayList anotherList) that adds all the elements in anotherList to the back of the calling list. Be sure to reallocate the data array if necessary. anotherList should not be modified. 2. (4 pts) Write a new method named...
Write a method named factorial that accepts an integer n as a parameter and returns the factorial of n, or n!. A factorial of an integer is defined as the product of all integers from 1 through that integer inclusive. For example, the call of factorial(4) should return 1 2 3 4, or 24. The factorial of 0 and 1 are defined to be 1. You may assume that the value passed is non-negative and that its factorial can fit...