Reputation: 25
This is my code which is shown as follows:
import java.util.*;
import java.math.*;
public class Exercise {
public static void main(String[] args){
ArrayList<Number> list = new ArrayList<>();
list.add(59);
list.add(47);
list.add(32);
list.add(43);
list.add(95);
list.add(36);
}
public static void sort(ArrayList<Number> list){
int iteration = list.size();
while (iteration >= 0){
for (int i = 0; i < list.size() - 1; i++){
for (int k = 1; k < list.size(); k++){
if (list.get(i).getValue() > list.get(k).getValue()){
Number temp = list.get(k);
list.set(k, list.get(i));
list.set(i, temp);
}
}
}
iteration --;
}
}
}
class Number{
double d;
Number(double d){
this.d = d;
}
double getValue(){
return d;
}
}
The error message is:
The method add(int, Number) in the type ArrayList is not applicable for the arguments (int)
The error corresponds from this part:
list.add(59);
list.add(47);
list.add(32);
list.add(43);
list.add(95);
list.add(36);
Can anyone give me a help for fixing the error? Thank you!
Upvotes: 2
Views: 4156
Reputation: 80
You are not adding Number class actually, instead you are trying to add Integer.
Try this
list.add(new Number(59));
list.add(new Number(47));
list.add(new Number(32));
list.add(new Number(43));
list.add(new Number(95));
list.add(new Number(36));
Upvotes: 0
Reputation: 90724
ArrayList<Number>
you have to add instances of Number
not int
.Since your Number
constructor expects double
not int
you have to fill it like:
list.add(new Number(59.0));
Upvotes: 0
Reputation: 137
The arraylist you are using is holding Number types. You need to store number objects in your list instead of integers.
Example :
ArrayList<Number> list = new ArrayList<>();
Number num = new Number(12.0);
list.add(num);
Upvotes: 1