• 03-24-2011, 09:18 PM
Andrew_2
Hii every body ,

i want to count the odd number and find the largest number

Code:

```    public static void main(String[] args)     {           //GenerateArray();         //Scanner in = new Scanner(System.in);         LinkedList L1 = new LinkedList();         LinkedList L2 = new LinkedList();         int arr[] = new int[10];         for(int k=1;k<=arr.length;k++)         {         int rand = GenrateRandom();         L1.addFirst(rand);         }         System.out.println(L1);         int arr2[] = new int[15];         for(int k=1;k<=arr2.length;k++)         {         int rand = GenrateRandom();         L2.addFirst(rand);         }         System.out.println(L2);         CountOdd(L1);         CountOdd(L2);     }         public static int GenrateRandom()     {         int a =0;         int size = 60;         ArrayList<Integer> list = new ArrayList<Integer>(size);         for(int i = 20; i <=size; i++)         {         list.add(i);         }         Random rand = new Random();         while(list.size() > 0)         {                 int index = rand.nextInt(list.size());                   a = list.remove(index);         }         //System.out.println("Selected: "+    a);         return a ;     }     public static int CountOdd(LinkedList L)     {         int count = 0 ;         for(int i =0;i<L.size();i++)         {                                 count++;         }         System.out.println(count);         return count;     }             public static int findLargest(LinkedList L)     {             }         public static int Common(LinkedList L1,LinkedList L2)     {     } }```

MY question is : which method in the java.util.LinkedList can help me to get each node to see if it's odd or not , i know how i can check if it's odd or not and also how i can get largest element , but as i said i don't know the method that help me test each NODE.. will you help me please.
• 03-24-2011, 10:19 PM
Iron Lion
I'm not sure why you want to use a LinkedList for this, but a glance at the API shows a number of methods that will get a value from it, such as get(), poll(), pop() and removeFirst().
• 03-24-2011, 10:31 PM
Andrew_2
if((Integer)L.get(i)%2==1);
count++;

i used this one but it still does not work .. c'uase the rest methods are just for delete
and the reason of using Linked List is a requirement to solve this question.
• 03-25-2011, 12:04 AM
Iron Lion
Why are you getting an element from a LinkedList of Integers and casting the result to Integer?

The reason the above code snippet doesn't work is because of the semicolon after the if statement. This results in a blank statement being executed if the condition is true, and count being incremented regardless of the condition. Instead, use:

Code:

```if(L.get(i)%2==1) {     count++; }```
Note: this would also work without the curly brackets, but it's good practice to use them.
• 03-25-2011, 05:40 AM
Andrew_2
OOH! Sorry my fault .. i wrote it quickly emm i have to put integer like this because the elements in the list are object and this operator % does not work with Objects.

any how thanks a lot man , i really appropriated