Results 1 to 6 of 6
  1. #1
    hai789 is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default Looping ArrayList

    array t1={2,6,4,9}


    can anyone give a for or while loop code such that i need the largest and the secondlargest values from this array



    ie the values 6,9

  2. #2
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    array t1={2,6,4,9}

    what is array is this a Class
    java.sql.Array or reflect.Array :confused:
    i am the future

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    In simple way, loop the array on each element and at the same time maintain two variables to hold largest values.

  4. #4
    abhishek paul is offline Member
    Join Date
    Apr 2008
    Posts
    3
    Rep Power
    0

    Default

    ...in case you think of replacing the simple array with a ArrayList obj you can very well go for the Collections.max(Collection obj) to get the maximum value.

    Regards,
    Abhishek.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Something like this may help in that sense, I think.

    Java Code:
        private void findMax(ArrayList arl) {
            int max = 0;
            for(int i = 0; i < arl.size(); i++) {
                if(Integer.parseInt((String) arl.get(i)) > max) {
                    max = Integer.parseInt((String) arl.get(i));
                }
            }
            
            System.out.println(max);
        }

Similar Threads

  1. Can't solve error message while looping
    By BHCluster in forum New To Java
    Replies: 15
    Last Post: 04-22-2008, 10:51 AM
  2. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  3. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 08:48 PM
  4. Java Looping and decision
    By susan in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 04:29 AM
  5. looping a function
    By Username in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 05:37 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
  •