How to Find the Index of an Element in an Array in Java

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.

Finding the index of the smallest element in an array (Java)

The error is self explanatory. You fail to handle the case of empty input array.

public static int indexOfSmallest(int[] array){

// add this
if (array.length == 0)
return -1;

int index = 0;
int min = array[index];

for (int i = 1; i < array.length; i++){
if (array[i] <= min){
min = array[i];
index = i;
}
}
return index;
}

If the smallest element appears multiple times, and you want to return the index of its first occurrence, change your condition to:

if (array[i] < min) 

How to find array index of largest value?

public int getIndexOfLargest( int[] array )
{
if ( array == null || array.length == 0 ) return -1; // null or empty

int largest = 0;
for ( int i = 1; i < array.length; i++ )
{
if ( array[i] > array[largest] ) largest = i;
}
return largest; // position of the first largest found
}

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.

I want to find the index of the string in an array

You need to compare the value of inName with each of the values stored in the array, not with the array itself. You access each of the values stored in the array using the index starting with 0.

for (i = 0; i < names.length; i++) {
if (inName.equals(names[i])) {
System.out.println("Data found at [" + i + "]");
break;
}
}

// If the value stored in `inName` is found, the value of `i` will not reach up
// to the value equal to `names.length` because of the `break` statement. If the
// value of `i` has reached there, it means that the value stored in `inName`
// has not been found.
if (i == names.length) {
System.out.println("Data not found!");
}

Complete program:

import java.util.Scanner;

public class StringSearch {
public static void main(String[] args) {
int i;
Scanner sc = new Scanner(System.in);
String[] names = new String[5];

for (i = 0; i < names.length; i++) {
System.out.print("Enter name " + (i + 1) + " > ");
names[i] = sc.nextLine();
}

System.out.print("Input Name to compare > ");
String inName = sc.nextLine();

for (i = 0; i < names.length; i++) {
if (inName.equals(names[i])) {
System.out.println("Data found at [" + i + "]");
break;
}
}

// If the value stored in `inName` is found, the value of `i` will not reach up
// to the value equal to `names.length` because of the `break` statement. If the
// value of `i` has reached there, it means that the value stored in `inName`
// has not been found.
if (i == names.length) {
System.out.println("Data not found!");
}
}
}

A sample run:

Enter name 1 > John
Enter name 2 > Harry
Enter name 3 > Sam
Enter name 4 > Cristina
Enter name 5 > Manny
Input Name to compare > Sam
Data found at [2]

Find the index of 2nd occurence of an element in an Integer ArrayList using streams in java

Find the index of 2nd occurence of an element in an Integer ArrayList using streams in java

You could do it like this. If the item or itemNo is not found, it returns -1 via an Optional.

  • create a Map<Integer,List>
  • the key is the list element, the value is a list of indices at which it occurs.
  • then just get the list for the element item and the specify itemNo as the ordinal value (2nd in this case) to get.
List<Integer> list = Arrays.asList(1, 0, 0, 1, 0, 1);
int item = 1; // for item 1
int itemNo = 2; // get the second value
Optional<Integer> index = IntStream.range(0, list.size()).boxed()
.collect(Collectors.groupingBy(list::get))
.getOrDefault(item, List.of(0))
.stream().skip(itemNo-1).findFirst();

System.out.println(index.orElseGet(()->-1));

prints

3

Of course, in practice, one would just use a loop.

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..



Related Topics



Leave a reply



Submit