-2
 if ((list.get(i)) > (list.get(maxInd))) {
     maxInd = i;
 }

error giving in on operator, where list is an object of LinkedList and MaxInd contains the first element of linked list while i is the variable of for loop


  LinkedList list = new LinkedList();
    list.add(2);
    list.add(1);
    list.add(3);

    int maxInd = 0;
    int list_size = list.size();
    for (int i = 0; i < list_size; i++) {
        if((list.get(i)) > (list.get(MaxInd))) {
            maxInd = i;
        } // end if condition    
    } // end for loop
Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36

1 Answers1

2

The issue is that your LinkedList uses a raw type, meaning that the elements of the list are treated as Object types. The simplest solution is to use a type parameter:

LinkedList<Integer> list = new LinkedList<Integer>();
// ...

Or you can use a cast, but only only if you're sure that all the elements in the list are Integers:

if ((Integer)(list.get(i)) > (Integer)(list.get(MaxInd))) {
    MaxInd = i;
}
Community
  • 1
  • 1
TNT
  • 2,900
  • 3
  • 23
  • 34