Need some help with this java problem, thanks!
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class ListUtil {
public List<Integer>
minToFront(List<Integer> list) {
List<Integer> copy = new
ArrayList<Integer>(list);
int min =
Collections.min(copy);
copy.remove(min);
copy.add(0, min);
return copy;
}
public List<Integer>
filterRange(List<Integer> list, int min, int max) {
List<Integer> copy = new
ArrayList<Integer>(list);
Iterator<Integer> itr =
copy.iterator();
while(itr.hasNext()) {
int value =
itr.next();
if(value == min
|| value == max || (value > min && value <
max))
itr.remove();
}
return copy;
}
public List<Integer>
intersect(List<Integer> list1, List<Integer> list2)
{
List<Integer> copy1 = new
ArrayList<Integer>(list1);
List<Integer> copy2 = new
ArrayList<Integer>(list2);
List<Integer> intersection =
new ArrayList<Integer>();
Collections.sort(copy1);
Collections.sort(copy2);
Iterator<Integer> itr1,
itr2;
if(copy1.size() > copy2.size())
{
itr1 =
copy1.iterator();
itr2 =
copy2.iterator();
} else {
itr1 =
copy2.iterator();
itr2 =
copy1.iterator();
}
int i = itr1.next();
int j = itr2.next();
while(itr1.hasNext()) {
if(i == j)
{
if(!intersection.contains(i)) {
intersection.add(i);
i = itr1.next();
j = itr2.next();
}
}
else if(i <
j)
i = itr1.next();
else
j = itr2.next();
}
intersection.add(i);
return intersection;
}
public static void main(String[] args) {
ListUtil listUtil = new
ListUtil();
List<Integer> list =
Arrays.asList(new Integer[] {4, 7, 8, 9, 1});
List<Integer> list1 =
Arrays.asList(new Integer[] {4,7,1,3,5});
System.out.println("After minimum
to Front:" + listUtil.minToFront(list));
System.out.println("After filtered
via range:" + listUtil.filterRange(list, 5, 7));
System.out.println("After
intersection:" + listUtil.intersect(list, list1));
}
}
Need some help with this java problem, thanks! Failure to submit will result in a ZERO...
*Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...
program is C# Create a program to help some local coffee shops manage their menus Requirements The class contains: 1) A method called defaultMenu that returns a String list and has no parameters. When called, the method returns a string list containing the following elements: {"Coffee", "Americano", Cappuccino" A method called custom Menu that returns a String list and has an int parameter. When called, the method will create a new string list, then ask the user for items to...
write a completed JAVA program (Main included) and make it not interactive but straight to the output Write a method called partition that accepts a list of integers and an integer value E as its parameter, and rearranges (partitions) the list so that all the elements with values less than E occur before all elements with values greater than E The exact order of the elements is unimportant, so long as all elements less than E appear before all elements...
1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...
by netBeans C++ java by netBeans C++ java by netBeans C++ java 1. Answer the following question a) (4 marks) Write a Java method, which takes an integer array and two integers x and y as input parameters. The method should exchange the value in position x with the value in the position y in the array. Example: If x = 1, y=2 Input Array Output Array b) (4 marks) Write the main method to do the following: 1. Define...
1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...
(intro to java help?) Write a method manyStrings that takes an ArrayList of Strings and an integer n as parameters and that replaces every String in the original list with n of that String. For example, suppose that an ArrayList called "list" contains the following values: ("squid", "octopus") And you make the following call: manyStrings(list, 2); Then list should store the following values after the call: ("squid", "squid", "octopus", "octopus") As another example, suppose that list contains the following: ("a",...
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...
Hello, I need help writing the two methods to print a triangle shape in java. An example of the shape is as follows: 9 8 7 6 5 4 3 8 7 6 5 4 3 7 6 5 4 3 6 5 4 3 5 4 3 4 3 3 One method should be defined as "public static void printPattern(int num1, int num2, Boolean ascending)". This method will print a upper-triangle matrix-like layout filled will a sequence of integers...
daily21. Computing 1. C programming Please provide comments and write pre-condition and post-condition for the function. Description Create a project called Daily21. Add a source file called daily21.c into the project. In this exercise, you will practice working with an array of integers. In the main function, you first need to create an integer array with 10 elements in t. Use a loop to read 10 integers from the user and store the values to the array (in the order...