3

I have an atomic integer array of size 10. I am using this array to organize numbers 1-10 sent in by threads. This 1-10 will eventually be able to change to be a range of numbers larger than 10 and the list is to contain the 10 greatest numbers in that range. I can see the numbers going into the loops and recognizing that they are greater than a number currently there. However, there is never more than 2 numbers in the array when it is printed out. I have tried to trace my code in debug mode, however, it looks as if it is working as intended to me. I feel like there may be a simple error to my logic? I am completely sure all values are entering in the function as I have triple checked this. I start at the end of the array which should contain the highest value and then swap downwards once the slot has been determined. I would appreciate the assistance. This is just a simple experiment I am doing in order to grasp the basics before I try to tackle a homework assignment.

Here an example of my code:

 public class testing{

            static AtomicIntegerArray maxList = new AtomicIntegerArray(10); 
            final static int n = 10;

    static void setMax(int value)
            {

            for(int i = 9; i >= 0; i--)
            {       
                if(value > maxList.get(i))
                { 
                    int temp = maxList.get(i);

                maxList.set(i,value);

                if(i == 0)
                {
                    maxList.set(i, value);
                }

                else
                {   for(int j = i-1; j > 0; j--)
                    {
                        maxList.set(j, temp);
                        temp = maxList.get(j-1);
                    }
                }
                break;
            }
        }


    public static void main(String[] args)
    {
        for (int i = 0; i < n; i++)
        {
            setMax(i);
        }
    }
}

Here is an example of how it is being called:

Brooke
  • 109
  • 1
  • 2
  • 12
  • Why not use a `SortedList`? Also please consider creating and posting a valid [mcve] or [SSCCE](http://sscce.org). Please read the links for the details. – Hovercraft Full Of Eels Jan 27 '18 at 18:19
  • @HovercraftFullOfEels I need to prevent multiple threads from changing it at the same time. When the range gets bigger than 10, there is no way of knowing if the value you're trying to add is supposed to be in that sorted list. It may be less than all of the elements. I thought I had provided that type of example. I'll take another look at it. – Brooke Jan 27 '18 at 18:29
  • No, your code is not a [mcve] or [SSCCE](http://sscce.org). True examples are small but complete programs, complete with imports and main methods. There's no way to run your current code as is. – Hovercraft Full Of Eels Jan 27 '18 at 18:31
  • @HovercraftFullOfEels i'll work towards fixing that. – Brooke Jan 27 '18 at 18:35
  • 1
    Thanks. Not my down-vote by the way, but you do want to make your question as helpful to others with similar problems as possible. – Hovercraft Full Of Eels Jan 27 '18 at 18:37
  • @HovercraftFullOfEels I just want to learn how to make the examples correctly :). Does the most recent edit look better? – Brooke Jan 27 '18 at 18:41
  • 1
    @Brooke, Yes. Much better. – Taslim Oseni Jan 27 '18 at 18:43
  • Yes, better, thanks. – Hovercraft Full Of Eels Jan 27 '18 at 18:57

1 Answers1

7

Brooke, there is a small bug in your 'j' loop. You had saved the state of a variable (temp), however your logic in the j loop lost the state. This new logic preserves the state of the previous element in the list.

Try this:

for (int j = i - 1; j >= 0; j--) {
    int t2 = maxList.get(j);
    maxList.set(j, temp);
    temp = t2;
}
Ian Mc
  • 5,656
  • 4
  • 18
  • 25
  • Adding that extra variable into the mix worked perfectly. Thank you for catching that. I figured it was something small, but I couldn't figure it out for the longest time. I can see why it was not working properly before because of the way my variable was positioned and the index it was grabbing. – Brooke Jan 27 '18 at 18:34