0

I am trying to write a method which takes an int argument and returns the element of an array given if the passed int argument is less than the array length. I would appreciate it if someone can point me in the right direction.

Code:

String[] fruits = {"jan","feb","mar","apr","may","jun","jul","aug","sept","oct"};



public String getStr( int pos ){

    String str;

    if( pos <= fruits.length){

        for( int i=0; i < fruits.length; ++i){          
            if(fruits[i] == pos){

                str = fruits[pos];

            }//IF
        }
    }//IF
    else{
        str = null;
    }
    return str;
}//METOD
5
  • 2
    pos vs position. Which one is it? Also, What do you think fruits[i] does? Commented Feb 12, 2014 at 6:24
  • if( pos <= fruits.length){ return fruits[pos-1]; } Commented Feb 12, 2014 at 6:25
  • method should return void either any data type not both :public void String getStr Commented Feb 12, 2014 at 6:30
  • if(fruits[i] == position) fruits[i] return string and pos is integer how u r comparing this ..? Commented Feb 12, 2014 at 6:32
  • Thank you for all your help, now I know what I was doing wrong, and how I should be coding it. Thx Commented Feb 12, 2014 at 6:39

6 Answers 6

2
  1. Array data structure provides random access based on array index. Therefore, you dont need to iterate over the array until you reach the required position. You can access it directly.
  2. In Java (like most programming languages), arrays are 0-indexed. This means that for an array of size n, the indices of the array will vary from 0 to n-1. Therefore, any index <0 or index>=n will result in ArrayIndexOutofBounds exception.

Keeping in mind these things, the following code will work as desired: (assuming that 'pos' represents number based on 0-indexed counting)

String[] fruits = {"jan","feb","mar","apr","may","jun","jul","aug","sept","oct"};
public String getStr(int pos) {
    if( pos < fruits.length && pos > -1){
        return fruits[pos];
    }
    return null;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Why return when its void method?
Sorry about that. Directly copied from question code and did not pay attention.
that would just depend on how you use 'pos'. I am assuming that count of pos starts from 0.
Yes, it does start from 0
1

An array element can be accessed by the index. So you just have to check if the given index is within the array's bounds and then either return the array element or null. This can be written in a single line:

condition ? value_if_true : value_if_false

So, your method will look like this:

public String getStr(int pos) {
    return (pos >= 0 && pos < fruits.length) ? fruits[pos] : null;
}

Comments

0

Check that pos wich is index of array is valid then send element else null.

return (pos >-1 && pos < fruits.length) ? fruits[pos] : null;

Comments

0

Why do you need to iterate. Arrays are there so that you can find an element by index.

Just use

if( pos <= fruits.length){
    str = fruits[pos];
}//IF
else{
    str = null;
}

Comments

0

Ask yourself these questions:

  • How do you get a specific element in an array?
  • How would you check if the pos parameter is too large or too small (hint: use length)

just think about those two questions and try to put them into code. Others have already given the answer (which they shouldn't have because you didn't request it), but still ask yourself these questions to improve your understanding of the situation.

Comments

0
public void String getStr(int pos) {
    String str;

    if (pos >= 0 && pos < fruits.length)
    {
        return fruits[pos];
    }
    else
    {
       return null;
    }
} // METHOD

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.