How do you find the position of an element in an ArrayList?

How do you find the position of an element in an ArrayList?

Java.util.Arraylist.indexOf() in Java

The
indexOf()
method of
ArrayList
returns the index of the
first occurrence
of the specified element in this list, or
-1
if this list does not contain the element.

Syntax :

public int IndexOf(Object o)
obj :
The element to search for.

// Java code to demonstrate the working of

// indexOf in ArrayList

// for ArrayList functions

import java.util.ArrayList;

public class IndexOfEx {

public static void main(String[] args) {

// creating an Empty Integer ArrayList

ArrayList<Integer> arr = new ArrayList<Integer>(5);

// using add() to initialize values

arr.add(1);

arr.add(2);

arr.add(3);

arr.add(4);

// printing initial value

System.out.print(“The initial values in ArrayList are : “);

for (Integer value : arr) {

System.out.print(value);

System.out.print(” “);

}

// using indexOf() to find index of 3

int pos =arr.indexOf(3);

// prints 2

System.out.println(“\nThe element 3 is at index : ” + pos);

}

}

Output:

The initial values in ArrayList are : 1 2 3 4 The element 3 is at index : 2

Practical Application :

The index functions are mostly useful to
determine last or first occurrence of events, for example last occurrence of 6 in a throw of a die, or 1st occurrence of any letter in a name etc.

One more example:

// Java code to demonstrate the application of

// index functions in ArrayList

// for ArrayList functions

import java.util.ArrayList;

public class AppliIndex {

public static void main(String[] args) {

// creating an Empty Integer ArrayList

ArrayList<Integer> arr = new ArrayList<Integer>(10);

// using add() to initialize dice values

arr.add(1);

arr.add(2);

arr.add(4);

arr.add(6);

arr.add(5);

arr.add(2);

arr.add(6);

arr.add(1);

arr.add(6);

arr.add(4);

// using IndexOf() to find first index of 6

int pos1 =arr.indexOf(6);

// using lastIndexOf() to find last index of 6

int pos2 =arr.lastIndexOf(6);

// to balance 0 based indexing

pos1 = pos1+1;

pos2 = pos2+1;

// printing first index of 6

System.out.println(“The first occurrence of 6 is : ” + pos1);

// printing last index of 6

System.out.println(“The last occurrence of 6 is : ” + pos2);

}

}

Output:

The first occurrence of 6 is : 4 The last occurrence of 6 is : 9

This article is contributed by
Shambhavi Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Article Tags :

Java

Java – util package

Java-ArrayList

Java-Collections

Java-Functions

Practice Tags :

Java

Java-Collections

Baca Juga :   Prinsip utility dalam pembuatan karya kerajinan dari bahan kertas bermakna

Method indexOf() Signature

public int indexOf(Object o)

This method returns -1 if the specified element is not present in the list.

Java ArrayList.indexOf() Method

Last update on February 26 2020 08:07:31 (UTC/GMT +8 hours)

public int indexOf(Object o)

The indexOf() method is used to get the index of the first occurrence of an element in an ArrayList object.

Package:
java.util

Java Platform:
Java SE 8

Syntax:

indexOf(Object o)

Parameters:

Name Description
o The element whose index is to be returned.

Return Value:

The index of the first occurrence an element in ArrayList, or -1 if the element does not exist.

Return Value Type:
int

Pictorial presentation of ArrayList.indexOf() Method

Example: ArrayList.indexOf Method

The following example creates an ArrayList containing various colors. It then tries to determine the position of a certain color.

import java.util.*; public class test { public static void main(String[] args) { // create an empty array list with an initial capacity ArrayList<String> color_list = new ArrayList<String>(5); // use add() method to add values in the list color_list.add(“White”); color_list.add(“Black”); color_list.add(“Red”); color_list.add(“Green”); color_list.add(“Yellow”); System.out.println(“Size of list: ” + color_list.size()); // Print the colors in the list for (String value : color_list) { System.out.println(“Value = ” + value); } // Get the index of element “White” int pos=color_list.indexOf(“White”); System.out.println(“The color White is at index ” + pos); // Get the index of element “Green” pos=color_list.indexOf(“Green”); System.out.println(“The color Green is at index ” + pos); } }

Output :

F:\java>javac test.java F:\java>java test Size of list: 5 Value = White Value = Black Value = Red Value = Green Value = Yellow The color White is at index 0 The color Green is at index 3

Baca Juga :   Dalil tentang perintah hormat dan patuh kepada guru

Java Code Editor:

Previous:contains Method
Next:lastIndexOf Method



Share this Tutorial / Exercise on :
Facebook
and
Twitter

  • New Content published on w3resource:
  • HTML-CSS Practical: Exercises, Practice, Solution
  • Java Regular Expression: Exercises, Practice, Solution
  • Scala Programming Exercises, Practice, Solution
  • Python Itertools exercises
  • Python Numpy exercises
  • Python GeoPy Package exercises
  • Python Pandas exercises
  • Python nltk exercises
  • Python BeautifulSoup exercises
  • Form Template
  • Composer – PHP Package Manager
  • PHPUnit – PHP Testing
  • Laravel – PHP Framework
  • Angular – JavaScript Framework
  • Vue – JavaScript Framework
  • Jest – JavaScript Testing Framework


1. ArrayList get() Method

ArrayList.get(int index) method returns the element at the specified position ‘index’ in the list.

1.1. Syntax

public Object get( int index );

1.2. Method Parameter

index – index of the element to return. A valid index is always be between 0 (inclusive) to the size of ArrayList (exclusive).

For example, if ArrayList holds 10 objects then a valid argument index will be between 0 to 9 (both inclusive).

1.3. Return Value

The get() method returns the reference of the object present at the specified index.

1.4. IndexOutOfBoundsException

An invalid index argument will cause IndexOutOfBoundsException error.

Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 4, Size: 4 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at com.howtodoinjava.example.ArrayListExample.main(ArrayListExample.java:12)

1. ArrayList.indexOf() method

This method returns the index of the first occurrence of the specified element in this list. It will return ‘-1’ if the list does not contain the element.

1.1. indexOf() method syntax

public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; }

Baca Juga :   Bagian persetujuan dalam struktur teks negosiasi ditunjukkan oleh dialog

1.2. indexOf() method parameter

object – the object which needs to be searched in the list for it’s first index position.

1.3. indexOf() return value

Return value is of int type.

  • index – first index position of element if element is found.
  • -1 – if element is NOT found.

Java ArrayList indexOf()

The Java ArrayList indexOf() method returns the position of the specified element in the arraylist.

The syntax of the indexOf() method is:

arraylist.indexOf(Object obj)

Here, arraylist is an object of the ArrayList class.


ArrayList.IndexOf Method

How do you find the position of an element in an ArrayList?

Posted by: pskji.org