0

In fact, I want to use "normOfVector()" method with Integer, and Float, but I couldn't

I get this error:

The method normOfVector(ArrayList) in the type TestMethod is not applicable for the arguments (ArrayList)

    package code;

import java.util.ArrayList;
import java.util.List;

public class TestMethod
{

    public static float normOfVector(ArrayList<Integer> list)
    {
        float sum = 0.0f;
        for (Integer i : list)
            sum = sum + (float) (i * i);
        // end for

        return (float) Math.sqrt(sum);
    }// end function


    public static void main(String[] args)
    {

        // In this case no Problem
        ArrayList<Integer> listI = new ArrayList<>();
        listI.add(0);
        listI.add(3);
        listI.add(5);
        listI.add(7);
        listI.add(2);
        float result = normOfVector(listI);

        // In this case will not work
        ArrayList<Float> listF = new ArrayList<>();
        listF.add(0.0f);
        listF.add(3.0f);
        listF.add(5.0f);
        listF.add(7.0f);
        listF.add(2.0f);
        result = normOfVector(listF);

    }// end main

}// end class

My point is:

I want to create a method that accept the two cases.

Thank you.

Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

2

In your first block your are computing numbers but storing them as objects. You should be storing them as Integer or Float, or maybe Double or Number, but not Object. Second in your cos(...) method you should limit what kind of parameters are accepted by changing the declaration to List<Integer>, List<Float> or List<? extends Number>. It may help to study up on java generics and especially using the PECS principle.

Community
  • 1
  • 1
cyber-monk
  • 5,470
  • 6
  • 33
  • 42
0

Based on your updated code.

public class TestMethod {

    public static double normOfVector(List<? extends Number> list) {
        double sum = 0.0;
        for (Number d : list)
            sum += d.doubleValue() * d.doubleValue();

        return Math.sqrt(sum);
    }    

    public static void main(String[] args) {
        List<Integer> listI = new ArrayList<>();
        listI.add(0);
        listI.add(3);
        listI.add(5);
        listI.add(7);
        listI.add(2);
        double result = normOfVector(listI);

        List<Float> listF = new ArrayList<>();
        listF.add(0.0f);
        listF.add(3.0f);
        listF.add(5.0f);
        listF.add(7.0f);
        listF.add(2.0f);
        result = normOfVector(listF);
    }
}

This is how I would write it using List<Double>

Map<String, Double> currentWord = new HashMap<>();
double first = main.getWords().get(firstWord);
for(Map.Entry<String, Double> entry : main.getWords()) {
    String secondWord = entry.getValue();
    double cosValue = vectors.cos(first, entry.getValue());
    if (cosValue >= percentage) {
        currentWord.put(secondWord, Math.round(cosValue * 100) / 100.0);
        break; // is there any point searching for another word?
    }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Maybe you can also use something like this:

Number[] numbers = new Number[] { valueOfFloat, valueOfInteger };
davidddp
  • 561
  • 7
  • 12