Where Is Java's Array Indexof

Where is Java's Array indexOf?

There are a couple of ways to accomplish this using the Arrays utility class.

If the array is not sorted and is not an array of primitives:

java.util.Arrays.asList(theArray).indexOf(o)

If the array is primitives and not sorted, one should use a solution offered by one of the other answers such as Kerem Baydoğan's, Andrew McKinlay's or Mishax's. The above code will compile even if theArray is primitive (possibly emitting a warning) but you'll get totally incorrect results nonetheless.

If the array is sorted, you can make use of a binary search for performance:

java.util.Arrays.binarySearch(theArray, o)

How to find index of STRING array in Java from a given value?

String carName = // insert code here
int index = -1;
for (int i=0;i<TYPES.length;i++) {
if (TYPES[i].equals(carName)) {
index = i;
break;
}
}

After this index is the array index of your car, or -1 if it doesn't exist.

How to find the index of an element in an array in Java?

In this case, you could create e new String from your array of chars and then do an indeoxOf("e") on that String:

System.out.println(new String(list).indexOf("e")); 

But in other cases of primitive data types, you'll have to iterate over it.

Trying to find index of string in array in java

Problem lies in names.indexOf(longString). Because names is of type String[] which is an array. This type String[] does not have a method definition named indexOf. As an alternative you can try java.util.Arrays.asList(theArray).indexOf(o)

So, to correct your code snip, you could rewrite it like

System.out.println("The Longest String is: " + longString + " With The Index Of" + java.util.Arrays.asList(names).indexOf(longString));

Read the JavaDoc for java.util.Arrays to brush up your knowledge on how to deal with arrays in java, using Java API.

Further, you can achieve the same by modifying the semantics of your code. This answer by Elliott Frisch has done that for you. Read that as well..

How to find indexOf in a String array in Java

Your characters may not be in your russian array.

You use toUpperCase(), which has not specified a Locale. Do specify a Locale, like new Locale("ru") to it, as it may convert your lower case letters incorrectly otherwise.

Also non alphanumerical characters should cause problems.

Try something like this:

private static final Locale RUSSIAN = new Locale("ru");

private static final String russian = "АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЫЬ" + // 41
"ЭЮЯ1234567890";

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

String output = "";
char[] input = textArea.getText().toUpperCase(RUSSIAN).toCharArray();
int index;

for (int i = 0; i < input.length; i++) {
index = russian.indexOf(input[i]);
System.out.printf("char: %c, index: %d%n", input[i], index);
if (index >= 0)
output += codeMorse[index] + " ";
}

textArea.setText(output);
button.setText("Конвертировано!");
}
});

Converted char[] to String to remove Arrays.asList()...

How to get index of value in array?

You can loop over the Strings in the array and find the index for which the String matches what you are looking for.

int index = -1;
for (int i=0; i<array.length;i++) {
if (array[i].equals(value)) {
index = i;
break;
}
}

Find index of array in ArrayList

The equals() implementation of the primitive Java array performs a reference equality check. Meaning, they're only considered equal if the references point to the same instance of the array.

You could write your own index-seeking method that uses the Arrays.equals() static method like so:

List<Integer[]> nodes = new ArrayList<Integer[]>();
nodes.add(new Integer[] {1,1});
Integer[] lookingFor = new Integer[] {1,1};
int index = -1;
for (int i = 0; i < nodes.size(); i++) {
Integer[] array = nodes.get(i);
if (Arrays.equals(lookingFor, array)) {
index = i;
break;
}
}
System.out.println(index); // 0


Related Topics



Leave a reply



Submit