How to find out the lowest value with a generic method? - java

I am trying to resolve an excercise about finding out the lowest value in array using loop. The excercise is about generics. But i have a hard to find out the solution. For an array of string i will use " if (minimum.compareTo(list[i]) > 0) ". I am stuck in how to do this with an array of integer. Any hint or help is very appreciate.
There is my code:
public class Excercise {
public static void main(String[] args) {
//Create an array
Integer[] intArray = { new Integer(45), new Integer(2), new Integer(6), new Integer(15) };
//print the lowest value
System.out.print(min(intArray));
// min(intArray);
}
public static <E extends Comparable<E>> E min(E[] list) {
E minValue = list[0];
for(int i = 1; i < list.length; i++) {
if(minValue.compareTo(list[i]) { <-- i get an error her
minValue = list[i];
return minValue;
}
}

compareTo doesn't return a boolean, so you can't use it as a condition of your if clause.
See http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo%28T%29
Instead, it an returns int. If the result is above zero, that means the first term is greater. If it is below, it is lesser.
So use if(minValue.compareTo(list[i])>0)) instead.
There are other bugs in the code (mostly typos). I will leave those to you.

because compareTo() method will return integer value
change
if(minValue.compareTo(list[i]) {
to
if(minValue.compareTo(list[i]) > 0) {

The proper method should be as follows:
public static <E extends Comparable<E>> E min(E[] list) {
E minValue = list[0];
for(int i = 1; i < list.length; i++) {
if( minValue.compareTo(list[i]) > 0) { //compareTo always returns an int
minValue = list[i];
}
}
return minValue; // returs the minimum after checking all the array
}

Related

Trying to find a Unique Element in the ArrayList Implementation in Java. A getUnique Method

public class MyArrayList<T> implements MyList<T>{
int num; //number of things in the list
T[] vals; //to store the contents
#SuppressWarnings("unchecked")
public MyArrayList() {
num = 0;
vals = (T[]) new Object[3];
}
public T getUnique(){
T distinct = null;
int count = 0;
for (int i=0; i<vals.length; i++){
distinct = vals[i];
for (int j = 0; j<vals.length; j++){
if (vals[j] == vals[i]){
count++;
}
if (count == 1){
return distinct;
}
}
}
if (distinct == null){
throw new IllegalArgumentException();
}
return distinct;
}
I am trying to work on a get Unique Method. A method getUnique that takes no arguments and returns the first value in the list that appears only once. (For example, calling the method on the list [1,2,3,1,2,4] would return 3 since 1 and
2 both appear more than once.) If the list is empty or all its values appear more than once, the method throws a NoSuchElementException
I have added some FIXME's to your code:
public T getUnique(){
T distinct = null;
int count = 0; // FIXME: move this initialization inside the i loop
for (int i=0; i<vals.length; i++){
distinct = vals[i];
for (int j = 0; j<vals.length; j++){
if (vals[j] == vals[i]){ // FIXME: use .equals() not ==
count++;
}
if (count == 1){ // FIXME: move this check outside the j loop
return distinct;
}
}
}
if (distinct == null){ //FIXME: no check needed, just throw it
throw new IllegalArgumentException();
}
return distinct; //FIXME: no valid return can reach this point
}
Patrick Parker's advice will fix your code, but I wanted to provide a cleaner and faster solution to the problem of finding a unique element in a list. This algorithm runs in time O(n) instead of O(n^2).
public static <T> Optional<T> getUnique(List<T> ls) {
// Create a map whose keys are elements of the list and whose values are
// lists of their occurences. E.g. [1,2,3,1,2,4] becomes {1->[1, 1],
// 2->[2, 2], 3->[3], 4->[4]}. Then elements.get(x).size() tells us how
// many times x occured in ls.
Map<T, List<T>> elements = ls.stream()
.collect(Collectors.groupingBy(x -> x));
// Find the first element that occurs exactly one time in ls.
return ls.stream().filter(x -> elements.get(x).size() == 1)
.findFirst();
}
You might call it like this:
Integer[] vals = {1,2,3,1,2,4};
System.out.println(getUnique(Arrays.asList(vals))
.orElseThrow(NoSuchElementException::new));
This code uses Java 8 streams and Optional. Below is another implementation of the same algorithm that doesn't use Java 8 language features; if you've never encountered streams, you may find it more understandable.
private static <T> T getUnique(List<T> arr) {
Map<T, Integer> numOccurrences = new HashMap<>();
for (T item : arr) {
numOccurrences.put(item, 1 + numOccurrences.getOrDefault(item, 0));
}
for (T item : arr) {
if (numOccurrences.get(item) == 1) {
return item;
}
}
throw new NoSuchElementException();
}

Is it possible to rewrite the following code without using .doubleValue()?

so I'm trying to write a method which uses generics. The method is suppose to takes a arrayList and return the highest value in the array. Here is the code to do that
public T largest() {
T value = myList.get(0);
for (int i = 1; i < myList.size(); i++) {
if (myList.get(i).doubleValue() > value.doubleValue()) {
value = myList.get(i);
}
}
return value;
}
Only problem is I'm not suppose to use .doubleValue. I've been stuck trying to figure it out for hours with no luck, Generics are reallyyyyy throwing me off for some reason. Am I suppose to somehow cast something? How can I write that above method without using .doubleValue()? Thanks!
I think this is what they had in mind when giving you this task.
The elements of your list need to be something that is Comparable.
This way you can compare them simply by calling compareTo.
This implementation is more general, more generic.
That is you don't need to limit yourself to using doubleValue in particular.
package test;
import java.util.Arrays;
import java.util.List;
public class Test001 {
public static void main(String[] args) {
Long a = largest(Arrays.asList(new Long[]{2L, 5L, 7L}));
Integer b = largest(Arrays.asList(new Integer[]{2, 5, 7}));
System.out.println(a);
System.out.println(b);
}
public static <T extends Comparable<? super T>> T largest(List<T> myList) {
T value = myList.get(0);
for (int i = 1; i < myList.size(); i++) {
if (myList.get(i).compareTo(value) > 0) {
value = myList.get(i);
}
}
return value;
}
}
You can only find the largest element if an ordering of the elements is defined. Since you're got giving a Comparator to the method to specify such an order, you're assuming "natural order", which means the elements must be Comparable.
Your function changes to the following (changed to static to make this standalone answer "complete"):
public static <T extends Comparable<T>> T largest(Iterable<T> myList) {
T largest = null;
for (T value : myList)
if (largest == null || value.compareTo(largest) > 0)
largest = value;
return largest;
}

Recursive Search Function Java

public int search(String type) {
for (int i = 0; i < size; i++) {
if (array[size-1-i].contains(type)) return i;
}
return -1;
}
I am having trouble doing a recursive function of this previous search function , can somebody help me ?
For a recursive function, a simple solution would be to pass in the value you want to search and the index to search at as parameters to the function.
Then you check
if the index passed in is greater than length of array , then return -1 (since we were not able to find the element.
if you can find the value pass in at the index passed in, if yes, just return that index ,
if not above 2 , then try to search it at next index.
For this recursive function, start at index 0 , by passing 0 when calling the function.
Example code -
public int search(String type, int index) {
if (index >= array.length) {
return -1;
}
else if(array[index].contains(type)) {
return array.length - i + 1; # assuming size from your function is array.length
}
else {
return search(type, index + 1)
}
}
It seems you want to write a recursive variant of this search function. I am not doing any optimization in your code as you need to take care of that. I have assumed few things to make your code compile and here is the code I have tried:
static String[] array = new String[] {"John", "Sam", "David"};
static int size = array.length;
public static void main(String[] args) {
int index = searchRecursive(0,"Sam");
System.out.println("Index: " + index);
}
public static int searchRecursive(int indexToCheck, String type) {
int result = -1;
if(indexToCheck<size) {
if(array[size-1-indexToCheck].contains(type)) {
result = indexToCheck;
} else {
result = searchRecursive(indexToCheck+1,type);
}
}
return result;
}
public static int searchIterative(String type) {
for (int i = 0; i < size; i++) {
if (array[size-1-i].contains(type)) return i;
}
return -1;
}

How to find index of STRING array in Java from a given value?

I wanted to know if there's a native method in array for Java to get the index of the table for a given value ?
Let's say my table contains these strings :
public static final String[] TYPES = {
"Sedan",
"Compact",
"Roadster",
"Minivan",
"SUV",
"Convertible",
"Cargo",
"Others"
};
Let's say the user has to enter the type of car and that then in the background the program takes that string and get's it's position in the array.
So if the person enters : Sedan
It should take the position 0 and store's it in the object of Cars created by my program ...
Type in:
Arrays.asList(TYPES).indexOf("Sedan");
String carName = // insert code here
int index = -1;
for (int i=0;i<TYPES.length;i++) {
if (TYPES[i].equals(carName)) {
index = i;
break;
}
}
After this index is the array index of your car, or -1 if it doesn't exist.
for (int i = 0; i < Types.length; i++) {
if(TYPES[i].equals(userString)){
return i;
}
}
return -1;//not found
You can do this too:
return Arrays.asList(Types).indexOf(userSTring);
I had an array of all English words. My array has unique items. But using…
Arrays.asList(TYPES).indexOf(myString);
…always gave me indexOutOfBoundException.
So, I tried:
Arrays.asList(TYPES).lastIndexOf(myString);
And, it worked. If your arrays don't have same item twice, you can use:
Arrays.asList(TYPES).lastIndexOf(myString);
try this instead
org.apache.commons.lang.ArrayUtils.indexOf(array, value);
Use Arrays class to do this
Arrays.sort(TYPES);
int index = Arrays.binarySearch(TYPES, "Sedan");
No built-in method. But you can implement one easily:
public static int getIndexOf(String[] strings, String item) {
for (int i = 0; i < strings.length; i++) {
if (item.equals(strings[i])) return i;
}
return -1;
}
There is no native indexof method in java arrays.You will need to write your own method for this.
An easy way would be to iterate over the items in the array in a loop.
for (var i = 0; i < arrayLength; i++) {
// (string) Compare the given string with myArray[i]
// if it matches store/save i and exit the loop.
}
There would definitely be better ways but for small number of items this should be blazing fast. Btw this is javascript but same method should work in almost every programming language.
Try this Function :
public int indexOfArray(String input){
for(int i=0;i<TYPES,length();i++)
{
if(TYPES[i].equals(input))
{
return i ;
}
}
return -1 // if the text not found the function return -1
}
Testable mockable interafce
public interface IArrayUtility<T> {
int find(T[] list, T item);
}
implementation
public class ArrayUtility<T> implements IArrayUtility<T> {
#Override
public int find(T[] array, T search) {
if(array == null || array.length == 0 || search == null) {
return -1;
}
int position = 0;
for(T item : array) {
if(item.equals(search)) {
return position;
} else {
++position;
}
}
return -1;
}
}
Test
#Test
public void testArrayUtilityFindForExistentItemReturnsPosition() {
// Arrange
String search = "bus";
String[] array = {"car", search, "motorbike"};
// Act
int position = arrayUtility.find(array, search);
// Assert
Assert.assertEquals(position, 1);
}
Use this as a method with x being any number initially.
The string y being passed in by console and v is the array to search!
public static int getIndex(int x, String y, String[]v){
for(int m = 0; m < v.length; m++){
if (v[m].equalsIgnoreCase(y)){
x = m;
}
}
return x;
}
Refactoring the above methods and showing with the use:
private String[] languages = {"pt", "en", "es"};
private Integer indexOf(String[] arr, String str){
for (int i = 0; i < arr.length; i++)
if(arr[i].equals(str)) return i;
return -1;
}
indexOf(languages, "en")

Java: Integer obj can't cast to Comparable

I'm having problems trying to pass an Integer object from a driver class as an argument for function of a SortedArray Generic class I created. From my driver class, I convert the user's int input into an Integer object to be cast onto Comparable of my SortedArray class.
I continue to receive the error: "Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to Comparable". I took a look at some of my classmates' source codes only to find little difference in setting the arguments/parameters yet they have their code working just fine. I've been looking for hours trying to find what error I've made and I still can't find why my Integer object can't be cast to Comparable.
Here's a bit from my SortedArray Class
public class SortedArray implements Comparable{
public int size;
public int increment;
public int top;
Comparable[] a = new Comparable [size];
public SortedArray(int initialSize, int incrementAmount)
{
top = -1;
size = initialSize;
increment = incrementAmount;
}
public int appropriatePosition(Comparable value)
{
int hold = 0;
if(top == -1)
{
return 0;
}
else
{
for(int i = 0; i <= top; i++)
{
if(a[i].compareTo(value) > 0)
{
hold = i;
break;
}
}
}
return hold;
}
public void insert(Comparable value) //The function that my driver class needs to access
{
//Shifting numbers to the top
if(full() == true)
{
Comparable[] tempArray = new Comparable[top + increment];
for(int i= 0; i< size; i++)
{
tempArray[i]= a[i];
a = tempArray;
}
size = top + increment;
}
if(a[appropriatePosition(value) + 1] != null)
{
for(int i = top; i < appropriatePosition(value); i--)
{
a[i + 1] = a[i];
}
}
a[appropriatePosition(value) + 1]= value;
}
Here's the code for my driver class that passes Integer Object insertObj as an argument for SortedArray's insert function.
public class IntDriver {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
//Creating variables
int data;
boolean check = false;
int choice;
int size = 5;
int increment = 3;
SortedArray b = new SortedArray(size, increment);
//Creating Menu
while(check == false)
{
System.out.println("Please choose through options 1-6.");
System.out.println("1. Insert\n2. Delete\n3. Clear\n4. Smallest\n5. Largest\n6. Exit");
choice = keyboard.nextInt();
switch(choice)
{
case 1:
System.out.println("Type the int data to store in array location.");
data = keyboard.nextInt();
Integer insertObj = new Integer(data);
b.insert(insertObj);// Here's where I lay "insertObj" as an argument for the SortedArray function.
System.out.println("The value " + data + " is inserted");
break;
The problem is that Integer extends java.lang.Comparable, then your Comparable is not a java.lang.Comparable. Look at the error message, your Comparable comes from the default package rather than java.lang:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to Comparable
That is, you can't cast java.lang.Integer to your own class
As mentioned by axtavt, the problem is that you have your own class Comparable. More specifically, what that means is that:
Integer.valueOf(1) instanceof java.util.Comparable == true
Integer.valueOf(1) instanceof Comparable == false
This means that somewhere in your code you have something like:
Object[] a = new Object[] {Integer.valueOf(1);};
Comparable x = (Comparable) a[0];
// or something equivalent, this is likely being passed through layers
// and not being done next to each other like this.
You need to change that to:
Object[] a = new Object[] {Integer.valueOf(1);};
java.util.Comparable x = (java.util.Comparable) a[0];
Even better, you should rename your Comparator class to something that doesn't collide with the standard classes in Java. In general, even though Java has namespacing, you should try to avoid your classes having the same name as the system classes to avoid exactly this kind of confusion.
I haven't put too much effort into this, but wouldn't it just be easier to use a SortedSet implementation (or its child interface NavigableSet) like TreeSet rather than write your own class? That is, unless you wanted duplicate elements...

Categories

Resources