Explain Codes LogoExplain Codes Logo

Where is Java's Array indexOf?

java
prompt-engineering
performance
stream-api
Anton ShumikhinbyAnton Shumikhin·Nov 17, 2024
TLDR

Java arrays don't come with a built-in indexOf method. You'll have to create your own. Here it is in a nutshell:

public static int indexOf(int[] array, int key) { for (int i = 0; i < array.length; i++) { // Is that you, key? Funny place we've met. if (array[i] == key) return i; } // If the key isn't found, return -1 like a lost treasure map return -1; }

Just use it like so:

int index = indexOf(new int[]{1, 2, 3, 4}, 3); // Voila, returns 2

A one-liner ready to be unleash its power on your code.

Primitive Arrays vs. Object Arrays

When you're dealing with primitives...

Remember, Arrays.asList() won't work directly on primitive arrays. It sees this array as a single entity, an object, a monolith.

Fear not! Convert your "monolith" into boxed equivalents first:

Integer[] boxedArray = {1, 2, 3, 4}; // indexOf ➡️ throwing a Box-ing match int index = Arrays.asList(boxedArray).indexOf(3); // Returns 2

Sorted arrays? More like sorted chocolates

Happy day! You have a sorted array like chocolates neatly placed in their box. Use Arrays.binarySearch(). It's lightning quick - O(log n):

int[] sortedArray = {1, 2, 3, 4}; int index = Arrays.binarySearch(sortedArray, 3); if (index >= 0) { // Found the rogue! } else { // Your chocolate is in another box }

The secret lies in negative returns - they're a sign from above (or below) indicating the element isn't present.

Arrays... meet Collections

Arrays are social now

With Arrays.asList(), your arrays can now mingle with other collections. They can use sophisticated methods like indexOf and lastIndexOf from the List interface:

List<Integer> list = Arrays.asList(1, 2, 3, 4); int index = list.indexOf(3); // Returns 2

Becareful though, once an array becomes a list, it can't be resized. It's a fixed-size list. It's like moving out but your room never gets any bigger.

Welcome to the extension family

Surprise, indexOf for arrays already exist in Apache Commons Lang and Guava libraries:

  • Apache Commons Lang's ArrayUtils.indexOf():
int index = ArrayUtils.indexOf(new int[]{1, 2, 3, 4}, 3); // Returns 2
  • Guava's Ints.indexOf() for primitives:
int index = Ints.indexOf(new int[]{1, 2, 3, 4}, 3); // Returns 2

Suddenly, arrays don't seem that primitive now, do they?

Playing Frankenstein: Custom indexOf

Your own creation

Craft your indexOf, the world is your oyster or in this case, your array. Add null checks, custom behavior, your CV:

public static <T> int indexOf(T[] array, T key) { if (key == null) { for (int i = 0; i < array.length; i++) { // You're null? I'm null too! if (array[i] == null) return i; } } else { for (int i = 0; i < array.length; i++) { // It's a match! You're equals! if (key.equals(array[i])) return i; } } // Seems like the key is playing hard to get return -1; }

Custom sorting party: Bring your own Comparator

Arrays.binarySearch() doesn't just accept arrays and keys. Bring your own Comparator for that custom sorting:

Integer[] array = {2, 4, 1, 3}; Arrays.sort(array, Comparator.reverseOrder()); // Partying in reverse order int index = Arrays.binarySearch(array, 3, Comparator.reverseOrder()); // Ends at 2

Performance Jugglery

Choose your fighter, wisely

The method you pick could make or break your performance. Let that sink in:

  • For tiny arrays, a simple for loop should do the job. Like mowing a small lawn.
  • binarySearch: Your go-to method for large, sorted arrays. Just don't use it on unsorted arrays, they'll give undefined results. Binary search on unsorted arrays is like looking for your socks in the washing machine while it's running.
  • Boxing primitives has a tiny overhead. If you're dealing with primitives, libraries like Guava are efficient.

Stream API: Because why not?

The power of Java 8 Stream API: One line, functional-style operations. It's like driving an automatic, but with benefits on large data sets:

int index = IntStream.range(0, array.length) .filter(i -> key.equals(array[i])) .findFirst() .orElse(-1); // Returns 2