简体   繁体   中英

How to convert a function into a method that extends an existing Java class

I have the following simple function that returns the position of a String in a string array ( String[] ).

private int getIndexOf(String needle, String[] haystack) {
    int indexOfNeedle = -1;
    for (int index=0;index<haystack.length;index++) {
        if (haystack[index].equals(needle)) {
            indexOfNeedle = index;
            break;
        }
    }
}

I want to convert this to be a method of the ArrayList class so that I can call it with:

String[] myArray = {"red","green","blue"};
int indexOfGreen = myArray.getIndexOf("green");

(I know there are existing libraries I can include that include indexOf but reinventing this is a lesson for me as well.)

I believe the approach will involve extends ArrayList and that is it probably the class that I need to extend so my function above will need to be modified to fit into that extension class...

Rather than using haystack in the method version I would also expect to be able to search inside this referring to the array object that the method was called from.

I'm also thinking that I may need to replace my String[] arrays with this new extended class of array (or can I use an override to include my new method in the excusing String Array class?).

As I say, this is to help me learn how to do this in general in Java so the exercise itself may seem a bit pointless and the point of the question isn't really to learn how to find the position of an element in an array - that's just by way of an example.

(This answer is based upon the comments, which say that the question is about extension functions)

Java extension functions

Java does not allow extension functions, but there are alternatives you can use instead

Alternatives

  • Wrapping
    • You can have a class which contains the original object, and passes through the existing ones you want to use, while implementing the new ones
  • Extension
    • You can extend the original object type, but then the instances must be of your new type, rather than the original one
    • You only need to include the new methods, the rest pass to the super type

Other languages

Some other JVM languages, like Kotlin, do support extension functions, but Java itself does not

Not a good idea.

The best practice is to have a more general type (interface) in the declaration.

void f(List<String> list) { ... }
List<String> list = new ArrayList<>();

This allows other implementations, and being used as parameter is more flexible/powerfull.

Trying to extend these all known classes/interfaces will cause artefacts like rewrapping old classes with new classes and so on.

Instead use static functions in a utility class. Like the Collections and Arrays classes. With a binarySearch and other goodies.

By the way, such functionality as mentioned, probably exists, and one would like to build upon existing general functionality, instead of reinventing the wheel.

int index = Arrays.asList(haystack).indexOf(needle);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM