-2

I am a newbie in Java programming and I have a trouble in my program. I have an array and i want to extract the max integer from it and return it to the main program. But without using ArrayList. I must not have 0 in this certain position when i print it so I cant just replace it. Take a look at what i did so far but i think that it is so wrong.

public int extractMax()
    {
        for (int i = 0; i < size; i++)
        {
            if(maxInsert == Queue[i])
            {
                Queue[i] = null;
                return maxInsert;
            }
        }   return -1;
    }
D.A.G.D
  • 23
  • 1
  • 11
  • i dont know what to do.... I did nothing so far – D.A.G.D Mar 27 '16 at 14:19
  • Why the downvotes? It's quite clear for me.. – Nikolas Charalambidis Mar 27 '16 at 14:21
  • I'm not sure I actually understand what you're trying to do? Are you trying to iterate through the array to find the largest integer, and once found return that value? Or are you trying to do what I just said but actually removing the largest value from the array as well? – suroh Mar 27 '16 at 14:28
  • Newbies usually have to receive the clear code with the detailed explanation to understand the principle of programming at all (I judgeaccording his reputation). I see nothing bad in this case. Moreover is so big problem to write few lines of a code? Downvoting is not a motivation (I don't ask for up-votes) – Nikolas Charalambidis Mar 27 '16 at 14:29
  • @Afflicted I am trying to do 2 things... First to remove the maxInsert from the array and then to return this maxInsert to the main program.. I know which is the maxInsert – D.A.G.D Mar 27 '16 at 14:31
  • If you allowed, you can use ArrayUtils. Take a look at here: http://stackoverflow.com/questions/642897/removing-an-element-from-an-array-java – rdonuk Mar 27 '16 at 14:50
  • Why are you using a queue? – Chris Gong Mar 27 '16 at 14:51
  • @DaneBrick its just the name of the array... – D.A.G.D Mar 27 '16 at 14:52
  • Check my answer out, is it what you are maybe looking for? – Chris Gong Mar 27 '16 at 14:56
  • @D.A.G.D If that's all you're trying to do than all you need to do, is find the largest value remove that value and using that values position in the array insert a new value that you want to replace it with. – suroh Mar 27 '16 at 15:58

3 Answers3

1

You cannot assign null if the array in a an array of primitives, you would see an error like this :

cannot convert from null to int

If the array is an array of objects (Integer for example), then assigning it to null would work, but I would suggest if you need to manipulate the entries of your array that you use a List instead.

For example :

List<Integer> integers = new ArrayList<>(Arrays.asList(new Integer[]{1,2,3,4,5,6,7,8,9,10}));
System.out.println(integers.size());
integers.remove(new Integer(3));
System.out.println(integers.size());

Would print :

10
9
Benoit Vanalderweireldt
  • 2,925
  • 2
  • 21
  • 31
0

So the logic here is to basically iterate through every element of the array to see if there is an element greater than the current greatest element (starts at the first element) in the array. If a greater element is found, then the max variable is reset to this new value and the loop carries on. If an even greater element is found, then the same thing happens by updating the max variable. To actually remove this element from the array, you can create a new array with a size of one less than the original size, copy all the elements up to the max into the new array, and shift all the elements to the right of the max by to the left by one. This is what it should look like.

 public int extractMax()
        {
            maxInsert = Queue[0];
            int maxIndex = 0;
            for (int i = 1; i < size; i++)//get the location of the max element
            {
                if(maxInsert < Queue[i])
                {
                    maxInsert = Queue[i];
                    maxIndex = i;
                }
            }
            int[] temp = new int[Queue.length - 1];
            for(int j = 0; j < maxIndex; j++){ //adding elements up until the max
                temp[j] = Queue[j];
            }
            for(int k = maxIndex; k < temp.length; k++){ //adding elements after the max
                temp[k] = Queue[k+1];
            }   
            Queue = temp; //queue is now this new array we've just made
            return maxInsert;
        }
Chris Gong
  • 8,031
  • 4
  • 30
  • 51
0

Basically you can not remove an element from an Array like in List objects. So create a new Array and add all elements in Queue except the bigger one to the new Array. And lastly, assign the new array to the Queue. Here is an example code:

public int extractMax()
{
    for (int i = 0; i < size; i++)
    {
        if(maxInsert == Queue[i])
        {
            removeFromArray(i);
            return maxInsert;
        }
    }   return -1;
}


private void removeFromArray(int i) {
    int[] newQueue = new int[Queue.length-1];

    int k = 0;
    for (int j = 0; j < newQueue.length; j++,k++) {
        if(i == j) {
            k++;
        }

        newQueue[j] = Queue[k];
    }

    Queue = newQueue;
}
rdonuk
  • 3,921
  • 21
  • 39