Skip to content

Commit 35652a8

Browse files
committed
update
1 parent d72429f commit 35652a8

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

JavaArrayList/Readme.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,57 @@ Output:
182182
ddd
183183
dddd
184184
ddddd
185+
```
186+
187+
### `lastIndexOf() `
188+
It is used to return the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.
189+
```
190+
ArrayList<Integer> arr = new ArrayList<Integer>(7);
191+
192+
// using add() to initialize values
193+
arr.add(10);
194+
arr.add(20);
195+
arr.add(30);
196+
arr.add(40);
197+
arr.add(30);
198+
arr.add(30);
199+
arr.add(40);
200+
int element = arr.lastIndexOf(30);
201+
if (element != -1)
202+
System.out.println("the lastIndexof of + " 30 is " + element);
203+
else
204+
System.out.println("30 is not in" + " the list;
205+
206+
// the lastIndexof of 30 is 5
207+
```
208+
209+
### `Object[] toArray()`
210+
It is used to return an array containing all of the elements in this list in the correct order.
211+
```
212+
List<Integer> al = new ArrayList<Integer>();
213+
al.add(10);
214+
al.add(20);
215+
al.add(30);
216+
al.add(40);
217+
218+
Object[] objects = al.toArray();
219+
220+
// Printing array of objects
221+
for (Object obj : objects)
222+
System.out.print(obj + " ");
223+
```
224+
**Note:** toArray() method returns an array of type Object(Object[]). We need to typecast it to Integer before using as Integer objects.
225+
```
226+
List<Integer> al = new ArrayList<Integer>();
227+
al.add(10);
228+
al.add(20);
229+
al.add(30);
230+
al.add(40);
231+
232+
// Error: incompatible types: Object[]
233+
// cannot be converted to Integer[]
234+
Integer[] objects = al.toArray();
235+
236+
for (Integer obj : objects)
237+
System.out.println(obj);
185238
```

0 commit comments

Comments
 (0)