Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java indexof array

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

indexof an array in java

import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        // create an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();

        // insert element to the arraylist
        numbers.add(22);
        numbers.add(13);
        numbers.add(35);
        System.out.println("Number ArrayList: " + numbers);

        // find the position of 13
        int position1 = numbers.indexOf(13);
        System.out.println("Index of 13: " + position1);

        // find the position of 50
        int position2 = numbers.indexOf(50);
        System.out.println("Index of 50: " + position2);
    }
}
Comment

index of function in java for array

// Java program to find index of
// an element in N elements
import java.util.stream.IntStream;
public class index {
  
    // Function to find the index of an element
    public static int findIndex(int arr[], int t)
    {
        int len = arr.length;
        return IntStream.range(0, len)
            .filter(i -> t == arr[i])
            .findFirst() // first occurrence
            .orElse(-1); // No element found
    }
    public static void main(String[] args)
    {
        int[] my_array = { 5, 4, 6, 1, 3, 2, 7, 8, 9 };
        System.out.println("Index position of 5 is: "
                           + findIndex(my_array, 5));
        System.out.println("Index position of 7 is: "
                           + findIndex(my_array, 7));
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: difference between class and interface 
Java :: java get current index in for loop 
Java :: writing values in 2d array java 
Java :: run java from terminal 
Java :: how to run java in eclipse 
Java :: java variable declaration 
Java :: java iterator index 
Java :: java Overridding example 
Java :: fastreader in java 
Java :: java actionevent 
Java :: centos 8 install java 16 
Java :: important features of java programming language 
Java :: how to add textview to listview in android 
Java :: list.of java 
Java :: java string character at index 
Java :: smart ways to print a matrix in java 
Java :: android studio remove button onclick 
Java :: sort 2d array based on one column java 
Java :: jcolorchooser in java 
Java :: public class HelloWorld{ public static void main(String []args){ System.out.println("Hello World"); } } 
Java :: android dialog modal 
Java :: java read file from command line argument 
Java :: how to get string input line in java 
Java :: android get string value 
Java :: java.lang.IllegalStateException: Could not obtain identifier 
Java :: practical1_answer 
Java :: android check free space programmatically 
Java :: slice() in typesript 
Java :: array list vs vector 
Java :: thread priorities in java 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =