Results 1 to 3 of 3
  1. #1
    stateofyouboi is offline Member
    Join Date
    Feb 2011
    Rep Power

    Default simple array processing(apparently!)

    any help at all with this would be great,ive spent hours googling yesterday but to no avail

    int [] nums = {10,20,10,20,60,10,5,60,5,60}

    1. (i)find the maximum in the array (ii)the position of where it occurs first and last
    2. find 2nd largest element in array
    3. find the maximum absolute difference between adjacent pairs of elements in the array
    4. determine IF 2 arrays are identical
    5. reverse the order of the array elements

    i was able to do q1 part (i) then i havent a clue!

    int highest = 0;

    for (int i=0; i<nums.length; i++) {
    if(nums[i] > highest)
    highest = nums[i];

    when it mentions determine if 2 arrays are identical i think it could be referring to my last exercise sheet which had the same array, im not sure. The lecturer will be going through this with us next week but i need all the help i can get so im trying to stay ahead of things if you know what i mean :) even help with one or two might point me int he right direction!

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Zurich, Switzerland
    Rep Power


    your 1. requirement is ok. for the 2. reguirement the implementation would be very easy if your array is already sorted, so before you assign the highest you could get the value inside highest and this value will be the 2nd largest. for sorting make a call to Arrays.sort(nums). the 3. can be also be implemented in simple for-loop. for 4. the question is, if you want to list all identical arrays. and for the 5. i would write a comparator the sort the ints in reverse order. for a ascending order the returned value is calculated by firstInt.compareTo(secondInt) and for a descending order a simply secondInt.compareTo(firstInt) can be used. but watch out: before you can call the compareTo you must convert your int to the class Integer. perhaps somebody can suggest an easier way.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power


    The code you posted found the highest value in the array. For part (ii) what you need is another variable, say highestNdx, that "remembers" the index where you set or updated the highest

    Java Code:
    int highest = 0;
    int highestNdx = 0;
    for (int i=0; i<nums.length; i++) {
    if(nums[i] > highest)
        highest = nums[i];
        highestNdx = ???

    If you can't use Arrays.sort() then the code to find the second highest is much the same as the code to find the highest value. All you need is a slightly more complicated condition for the if statement which expresses the condition "if current num is bigger than highest AND i is not equal to highestNdx" - basically you do search for the highest again ignoring the index position that you found initially.

    (With some ingenuity you could combine both searches into one for loop, but I don't think that's the intent here.)

Similar Threads

  1. instruction apparently not executed
    By rippon in forum AWT / Swing
    Replies: 4
    Last Post: 11-30-2010, 01:30 AM
  2. The field "name" doesnt apparently exsist :/
    By Addez in forum New To Java
    Replies: 6
    Last Post: 10-22-2010, 11:15 PM
  3. Processing input - Very simple but I can't do it
    By Hitsuin in forum New To Java
    Replies: 1
    Last Post: 08-23-2010, 03:31 PM
  4. My Simple Array Does Not Work!
    By Simplev_v in forum New To Java
    Replies: 16
    Last Post: 09-07-2009, 02:43 PM
  5. Simple array questions
    By jigglywiggly in forum New To Java
    Replies: 6
    Last Post: 02-15-2009, 05:57 AM

Posting Permissions

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