0

I need to write a single generic method which can take both an array and a String reference. Inside such method, depending on the argument, I need to check for equality as well as do indexing. For array, the indexing is of course [] operator, but for String, it is charAt() method. Besides, I want to iterate over the argument, so for array I will be using length field and for String I will be using length() method. For equality, I need to use equals() method for array and == for chars in the String.

Is it something I'd better off write two separate overloaded methods, or it is worth the efforts to make it a generic method.

If in the latter case, how to do this?

2
  • What exactly are you trying to achieve with this method? Commented Jan 17, 2012 at 20:20
  • Overload; you're taking different types. Commented Jan 17, 2012 at 20:22

4 Answers 4

1

Arrays in java are objects, so you can write a method that receive one Object and test using the "instanceof" function.

public class MethodTest {

public static void main(String[] args) {
    String[] tst = new String[]{"SOME","TEXT"};
    doSomething( tst );
    doSomething("TEST");
}

public static void doSomething( Object o ) {
    if( o instanceof String ) {
        System.out.println( (String) o );
    } else if( o instanceof String[] ) {
        String something[] = (String[]) o;
        for( String s : something ) {
            System.out.println( s );
        }
    }
}

}

But I think two methods will be a best approach...

Sign up to request clarification or add additional context in comments.

Comments

0

From your description of the problem, you're almost certainly better off writing two overloaded methods.

It is unclear what -- if anything -- would be gained by combining them into one. However, doing that would probably require a non-trivial amount of effort and the result will likely be pretty convoluted.

Besides, a single method that can take both an array and a String would have to take Object as its argument. Once you accept Object, people will be able to call your method with lots of things it won't know how to deal with; instead of compile-time failures, the combined method would have to fail at run time.

Comments

0

I guess you are trying to achieve the same for char[]-s and String-s. It does not worth to write the generic method (imo, because of the lack of operator overloading in java), but you can easily convert the char[] to String(or vice versa) then execute the same code, if it is ok.

2 Comments

not necessarily char[], I may use some fancy objects myObjs[] for which equals() is overriden. :)
In this case, definitely different methods. (Because of the lack of overloading), but I can't imagine a situation when you do the same thing for string literal types and "other" types.
0

Overload the method. The effort in solving that in a generic way will be waste and most likely unreadable.

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.