Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Finding the index of the maximum number in an ArrayList and removing the number that occupies that index

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    26
    My Mood
    Where
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default Finding the index of the maximum number in an ArrayList and removing the number that occupies that index

    Here is my code which works fine
    public class Arrays {
        public static void main (String[]args) {
     
        int maxNum= 0;
        int minNum = 100;
        int position=0;
        ArrayList <Integer> numbers = new ArrayList<Integer>();
        int i;
        for (i=0; i<5;i++) {
            numbers.add((int)(Math.random()*10)+1);  
        }
        Collections.sort(numbers);
        System.out.println(numbers);
        try {
        for (i=0;i<numbers.size();i++){
            if (numbers.get(i) > maxNum) { 
                position=i;
     
               maxNum = numbers.get(i);
            }
        }
         numbers.remove(position); //removes the index that contains the maximum number
        System.out.println("Maximum number: " +  maxNum);
        System.out.println("Found max at index " + position);
        System.out.println(numbers);
        }
        catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
        }}
    The problem is, sometimes random numbers containing two maximum numbers such as 4, 10, 2, 10,6 is generated.With what i have, only one of the 10s is removed.How do i make sure both are removed?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Finding the index of the maximum number in an ArrayList and removing the number that occupies that index

    If you're still trying to generate unique random numbers, I suggest you review my response to your other topic. A Set would be a much more convenient collection for ensuring uniqueness. That's what it's for. You're just reinventing that with what you're trying to do here.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Finding the index of the maximum number in an ArrayList and removing the number that occupies that index

    If you have multiple values as the maximum then find the maximum value instead of the index. Then iterate over the List again and delete all instances of that value.
    Improving the world one idiot at a time!

  4. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Finding the index of the maximum number in an ArrayList and removing the number that occupies that index

    Hello.
    I have an alternate solution. No matter whether you have duplicated values or not. It should work for both the scenarios.
    You have already sorted your collection in ascending order.
    Get the last value, cache it and delete it.
    Ex: int n=list.get(list.size()-1); list.remove(list.size()-1);
    Then check if the last value in the remaining list is again same as n. If yes repeat the above step. Do it until the last element of the remaining list is different than what was deleted earlier.

    Syed.

Similar Threads

  1. Replies: 2
    Last Post: April 9th, 2013, 12:39 AM
  2. Replies: 1
    Last Post: April 1st, 2013, 04:13 PM
  3. Need some help with removing and finding items from an ArrayList
    By bankston13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 30th, 2012, 08:51 PM
  4. ArrayList initial capacity problem (Index out of bounds Exception)
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 11:24 AM
  5. Search for number in array and return index
    By Kevinius in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2011, 12:00 AM