8 Replies - 273 Views - Last Post: 04 August 2013 - 07:46 PM Rate Topic: -----

#1 apples12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 03-August 13

Question on a java code for finding the last index of an array,

Posted 03 August 2013 - 01:00 PM

This method accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. The method should return -1 if the value is not found. For example, in the list containing {74, 85, 102, 99, 101, 85, 56}, the last index of the value 85 is 5.


public static int lastIndexOf (int[] arr, int n) {
    int i=0;
    int max=0;
    int lastIndex=0;
    int count=0;
    for(i=0 ; i<arr.length; i++) {
        if (arr[i]==n) {
            max=i;
            if (max>lastIndex) {
                lastIndex=max;
                 
                
            }
            
        }
        return lastIndex;  
    }
    return -1;
      
}



However when I try arguments such as lastIndexOf({10, 34, 56, 10, 90, 10, 68, 10}, 10)
lastIndexOf({12, 34, 56, 78, 90, 24, 68, 10}, 90)

it doesn't work gives me the wrong index. Thanks for helping :helpsmilie: :helpsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: Question on a java code for finding the last index of an array,

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: Question on a java code for finding the last index of an array,

Posted 03 August 2013 - 01:11 PM

The first return statement returns the current i no matter what. In other words, i = 1 is never checked. Perhaps that return statement is in the wrong place?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Question on a java code for finding the last index of an array,

Posted 03 August 2013 - 01:12 PM

Why don't you simply scan the array from the end ?

public static int lastIndexOf (int[] arr, int n) {
    for(i = arr.length; --i >= 0; ) {
        if (arr[i]==n)
           return i;
    }
    return -1;      
}


Was This Post Helpful? 1
  • +
  • -

#4 apples12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 03-August 13

Re: Question on a java code for finding the last index of an array,

Posted 03 August 2013 - 01:19 PM

View Postapples12, on 03 August 2013 - 01:00 PM, said:

This method accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. The method should return -1 if the value is not found. For example, in the list containing {74, 85, 102, 99, 101, 85, 56}, the last index of the value 85 is 5.


public static int lastIndexOf (int[] arr, int n) {
    int i=0;
    int max=0;
    int lastIndex=0;
    int count=0;
    for(i=0 ; i<arr.length; i++) {
        if (arr[i]==n) {
            max=i;
            if (max>lastIndex) {
                lastIndex=max;
                 
                
            }
            
        }
        return lastIndex;  
    }
    return -1;
      
}



However when I try arguments such as lastIndexOf({10, 34, 56, 10, 90, 10, 68, 10}, 10)
lastIndexOf({12, 34, 56, 78, 90, 24, 68, 10}, 90)

it doesn't work gives me the wrong index. Thanks for helping :helpsmilie:/> :helpsmilie:/>




Thank you greg and pbl, I think pbls method is much more simpler and logical but still would like to see a solution to my code thank you both
Was This Post Helpful? 0
  • +
  • -

#5 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 138
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Question on a java code for finding the last index of an array,

Posted 03 August 2013 - 01:31 PM

pbl just gave you a solution.

I don't even understand the point of your max variable. All you have to do is loop through the array, check if n == arr[i], if it is, return i. If it isn't return -1
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2774
  • View blog
  • Posts: 11,732
  • Joined: 20-September 08

Re: Question on a java code for finding the last index of an array,

Posted 03 August 2013 - 01:34 PM

As pbl says, it makes more sense to work backwards but your own code is over-complex. Initialise the result to -1 and assign to the result if and when equality exists between the test value and the current element
Was This Post Helpful? 1
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Question on a java code for finding the last index of an array,

Posted 03 August 2013 - 09:08 PM

If you still to go forward... your version is still over complicated max and count are useless
The simplest way would be

public static int lastIndexOf (int[] arr, int n) {
    int lastIndex = -1;
    for(int  i=0 ; i<arr.length; i++) {
        if (arr[i]==n) 
            lastIndex = i;
    }
    return lastIndex;  
      
}



:^:

*Edited: oups.... sorry.. this is exactly what g00se was saying in his previous post

This post has been edited by pbl: 03 August 2013 - 09:09 PM

Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2774
  • View blog
  • Posts: 11,732
  • Joined: 20-September 08

Re: Question on a java code for finding the last index of an array,

Posted 04 August 2013 - 03:42 AM

Quote

*Edited: oups.... sorry.. this is exactly what g00se was saying in his previous post
No problem ;)
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Question on a java code for finding the last index of an array,

Posted 04 August 2013 - 07:46 PM

I just provided the actual code ... what we shouldn't actually be doing :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1