use java code Write a collection of array utility methods.
•Write a method to create a list of duplicate numbers in an array.
•Write a method to create a list of duplicate Strings in an array.
•Convert to using generics.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// ArrayUtilties.java
import java.util.ArrayList;
import java.util.List;
public class ArrayUtilties {
/**
* method to find the list of duplicate numbers in an array of integers
*/
public static List<Integer> findDuplicateNumbers(int numbers[]) {
// creating a temporary list
ArrayList<Integer> tempList = new ArrayList<Integer>();
// creating a list to store duplicates
ArrayList<Integer> duplicates = new ArrayList<Integer>();
// looping through all elements in numbers
for (int i = 0; i < numbers.length; i++) {
// checking if current number is already present in tempList and not
// in duplicates.(i.e checking if current number is met before)
if (tempList.contains(numbers[i])
&& !duplicates.contains(numbers[i])) {
//adding to duplicates list
duplicates.add(numbers[i]);
} else {
//adding other numbers to tempList
tempList.add(numbers[i]);
}
}
//returning list of duplicates
return duplicates;
}
/**
* method to find the list of duplicate strings in an array of strings
*/
public static List<String> findDuplicateStrings(String strings[]) {
ArrayList<String> tempList = new ArrayList<String>();
ArrayList<String> duplicates = new ArrayList<String>();
for (int i = 0; i < strings.length; i++) {
if (tempList.contains(strings[i])
&& !duplicates.contains(strings[i])) {
duplicates.add(strings[i]);
} else {
tempList.add(strings[i]);
}
}
return duplicates;
}
/**
* method to find and return a generic list of duplicate elements in a
* generic array. Here type T can be any datatype
*/
public static <T> List<T> findDuplicates(T array[]) {
ArrayList<T> tempList = new ArrayList<T>();
ArrayList<T> duplicates = new ArrayList<T>();
for (int i = 0; i < array.length; i++) {
if (tempList.contains(array[i]) && !duplicates.contains(array[i])) {
duplicates.add(array[i]);
} else {
tempList.add(array[i]);
}
}
return duplicates;
}
public static void main(String[] args) {
// testing all three implementations
int numbers[] = { 10, 10, 10, 15, 1, 9, 8, 7, 66, 11, 1 };
System.out.println(findDuplicateNumbers(numbers));
String strings[] = { "oliver", "john", "felicity", "john", "barry" };
System.out.println(findDuplicateStrings(strings));
Double doubles[] = { 22.5, 66.6, 10.55, 22.5, 10.5, 66.6 };
System.out.println(findDuplicates(doubles));
}
}
/*OUTPUT*/
[10, 1]
[john]
[22.5, 66.6]
use java code Write a collection of array utility methods. •Write a method to create a...
Must use JOPTIONPANE. Using arrays and methods create a JAVA program. Create a java program that holds an array for the integer values 1-10. Pass the array to a method that will calculate the values to the power of 2 of each element in the array. Then return the list of calculated values back to the main method and print the values
Create a Cow class. Make cows and put them in a collection. Write a method that removes cows from the collection based on a) name, b) legs. using Java array list
In java please write a method List toList(String[] array) that converts its argument array of strings to a list of strings and then returns it.
write a code that includes the following:(PYTHON NOT JAVA) # This program uses the writelines method to save # a list of strings to a file. def main(): # Create a list of strings. # Open a file for writing. # Write the list to the file. # Close the file. # Call the main function.
In Java: Create an array of Integers with 10 elements, loop (use a for loop) through the array of integers printing their values to the screen. Create an array of Strings with 10 elements, loop (use a for loop) through the array of Strings printing their values to the screen. Finally, create an ArrayList of Integers. Use the loop from part 1 above and add each integer as you print it to the ArrayList as well, then start a final...
Write a Java program that will create an array of Strings with 25 elements. Input Strings from the user and store them in the array until either the user enters “quit” or the array is full. HINT: the sentinel value is “quit” all lowercase. “Quit” or “QUIT” should not stop the loop. HINT: you have to use the equals method (not ==) when you are comparing two Strings. After the input is complete, print the Strings that the user entered...
Code a Java program to create and use OO classes. Comment your code throughout. Create a Super class named Home Properties address SF (this is the number of Square feet) value (this is the dollar value of the home) Methods Set and get methods for all properties calcValue() - calculate the value as sf * 400 displayCondo() - displays all properties of a condo Create a sub class named Condo Properties HOAfee (home owners association fee) Methods Set and get...
Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDE Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....
1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...
Please complete this code for java Lab Write a program as follows: Create an array with 10 elements. The array should contain numbers generated randomly between 1 and 100 Ask the user to enter any number - Search the array to see if the user entered number is in the array If the user-entered number is in the array, print a 'match found' message. Also print at which index the match was found, else print a 'match not found' message...