0

I am stuck again with ArrayList. I already know how to compare two items in Array but I would like to do the same in list.

So just an example for the comparison.

import java.util.*;

public class ArrayIf {
  public static void main(String[] args) {

    List numbers = new ArrayList();

    numbers.add(5);
    numbers.add(9);

    if (numbers[1] >= numbers[0])  {
        System.out.println ("The second number in the lit is bigger the the first!");
    } else System.out.println("The second number in the list is not bigger than the fist!");
  }
}

I do know the "if" line is good for the array and not for list AND here is my problem. I looked for but I haven't found how can i get the 2 item from one ArrayList in one Statement working.

I tried

numbers.get(1);

numbers[1]

but non of the worked. Can anybody help how can i get the indexed value out for the if statement?

xingbin
  • 27,410
  • 9
  • 53
  • 103
  • Not an answer, but many would say that `List numbers` is evil, because it uses a raw type. Instead, you probably want `List numbers`. – Tim Biegeleisen Feb 25 '18 at 14:08

1 Answers1

1

It does not work because the compiler does not know you are putting/getting Integer into/from the list.

You can use:

if (numbers.get(1) >= numbers.get(0))

If you declare the numbers like this:

List<Integer> numbers = new ArrayList<Integer>();
xingbin
  • 27,410
  • 9
  • 53
  • 103
  • Thank you the answers guys working like a charm. Its nice to have proper answers even if I am beginner level. Thats what I find nice about this community now I can advance to my next exercise. Cheers – Super Mario Feb 25 '18 at 14:22
  • check here for more...https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – xingbin Feb 25 '18 at 14:24