How can I use List<object> with Integer and Float ?in java

محمد التويجري

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.

cyber-monk

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java - How can I add an object to a list of another object

How can I convert integer into float in Java?

How can I tell if a Java integer is null?

How can I use a Scala singleton object in Java?

How can i use same object in different classes in Java

How can I represent integer intervals in Java?

How can I use List of object with Stripes "option" tag?

Golang how can I multiply a integer and a float

How can I print a LinkedHashMap of <Object, Integer>?

How can I convert float to integer in Java

How can I convert a String to an Integer or Float in Crystal?

How can I convert the list string to a float

How can I use Java Generics to create a list of subclasses?

Given a list of integer numbers and a list of indices how can i use map to add 10 to specific elements in the first list?

how can I validate a list index as an integer?

code has to be a float so how can i convert it to a integer

How can I convert an integer to float value in textfield in swift ios?

How can I reinterpret a byte as an integer in java?

How can i use string variable as an object name in JAVA?

Can I use .Contains for Integer list?

How can I sort the float list

How can I create a List with sorted Strings and a specific Integer in Java?

how i can convert from int[] to List<Integer> in java

How can i convert my HashMap Key from float to Integer in java?

TypeError: 'list' object cannot be interpreted as an integer, when using a regex. How can I fix this?

How can i use an unknown class or a list as a parameter in java?

How can I make a list with the times that an integer occurs in another list?

Java how to check float minus float is integer?

'float' object cannot be interpreted as an integer and not add to a list