Results 1 to 5 of 5
  1. #1
    Andrew_2 is offline Member
    Join Date
    Mar 2011
    Posts
    36
    Rep Power
    0

    Default java.util.LinkedList;

    Hii every body ,

    am try to use java.util.LinkedList instead on create myLinkedList class.
    i want to count the odd number and find the largest number

    Java 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.
    Last edited by Andrew_2; 03-24-2011 at 09:19 PM.

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    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().

  3. #3
    Andrew_2 is offline Member
    Join Date
    Mar 2011
    Posts
    36
    Rep Power
    0

    Default

    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.

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    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:

    Java 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.
    Last edited by Iron Lion; 03-24-2011 at 11:08 PM.

  5. #5
    Andrew_2 is offline Member
    Join Date
    Mar 2011
    Posts
    36
    Rep Power
    0

    Default

    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

Similar Threads

  1. Java LinkedList Issue
    By Arunkumar in forum Advanced Java
    Replies: 6
    Last Post: 09-18-2009, 07:26 PM
  2. LinkedList-java
    By kamali in forum New To Java
    Replies: 2
    Last Post: 09-26-2008, 03:29 PM
  3. java.util
    By Java Tutorial in forum Java Tutorial
    Replies: 1
    Last Post: 02-07-2008, 01:46 PM
  4. Problem with LinkedList Java
    By paul in forum Advanced Java
    Replies: 2
    Last Post: 08-07-2007, 04:57 AM
  5. Please help me with LinkedList in Java
    By lenny in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 02:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •