-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseLastIndexOf.java
More file actions
40 lines (34 loc) · 1.23 KB
/
UseLastIndexOf.java
File metadata and controls
40 lines (34 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Java code to demonstrate the working of lastIndexOf() method in ArrayList
import java.util.ArrayList;
public class UseLastIndexOf {
public static void main(String[] args)
{
// creating an Empty Integer ArrayList
ArrayList<Integer> arr = new ArrayList<Integer>(7);
// using add() to initialize values
arr.add(10);
arr.add(20);
arr.add(30);
arr.add(40);
arr.add(30);
arr.add(30);
arr.add(40);
System.out.println("The list initially " + arr);
// last index of 30
int element = arr.lastIndexOf(30);
if (element != -1)
System.out.println("the lastIndexof of" +
" 30 is " + element);
else
System.out.println("30 is not present in" +
" the list");
// last index of 100
element = arr.lastIndexOf(100);
if (element != -1)
System.out.println("the lastIndexof of 100" +
" is " + element);
else
System.out.println("100 is not present in" +
" the list");
}
}