Skip to content

Commit d72429f

Browse files
committed
update
1 parent e907974 commit d72429f

1 file changed

Lines changed: 68 additions & 2 deletions

File tree

JavaArrayList/Readme.md

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import java.util.ArrayList; // import the ArrayList class
3232
```
3333

3434
### `add()`
35-
Add Items
35+
It is used to append the specified element at the end of a list.
3636
```
3737
ArrayList<String> cars = new ArrayList<String>();
3838
cars.add("Volvo");
@@ -42,7 +42,9 @@ ArrayList<String> cars = new ArrayList<String>();
4242
System.out.println(cars);
4343
```
4444
#### `add(int index, E element)`
45+
It is used to insert the specified element at the specified position in a list.
4546
```
47+
4648
List<String> colors = new ArrayList<>();
4749
colors.add("red"); // ["red"]
4850
colors.add("blue"); // ["red" , "blue"]
@@ -52,7 +54,12 @@ System.out.println(colors); // ["black", "red" , "white", "blue"]
5254
```
5355

5456
#### `addAll(Collection c)`
57+
58+
It is used to append all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
5559
```
60+
// boolean addAll(Collection<? extends E> c)
61+
// boolean addAll(int index, Collection<? extends E> c)
62+
5663
ArrayList<String> letters = new ArrayList<>();
5764
letters.add("A");
5865
letters.add("B");
@@ -101,7 +108,7 @@ cars.size();
101108
### `sort()`
102109

103110
- Sort an ArrayList in `Ascending Order`
104-
Another useful class in the java.util package is the Collections class, which include the sort() method for sorting lists alphabetically or numerically:
111+
Another useful class in the java.util package is the `Collections class`, which include the sort() method for sorting lists alphabetically or numerically:
105112
```
106113
ArrayList<String> cars = new ArrayList<String>();
107114
cars.add("Volvo");
@@ -116,4 +123,63 @@ ArrayList<String> cars = new ArrayList<String>();
116123
- Sort an ArrayList in `Descending Order`
117124
```
118125
Collections.sort(ArrayList, Collections.reverseOrder());
126+
```
127+
128+
### `isEmpty()`
129+
It returns true if the list is empty, otherwise false.
130+
```
131+
// creating an Empty Integer ArrayList
132+
ArrayList<Integer> arr = new ArrayList<Integer>(10);
133+
134+
// check if the list is empty or not using function
135+
boolean ans = arr.isEmpty();
136+
137+
if (ans == true)
138+
System.out.println("The ArrayList is empty");
139+
else
140+
System.out.println("The ArrayList is not empty");
141+
```
142+
143+
### `Iterator()`
144+
The ArrayList.Iterator () returns an iterator over the elements in this list.
145+
```
146+
ArrayList<String>arrlist = new ArrayList<String>();
147+
arrlist.add("d");
148+
arrlist.add("dd");
149+
arrlist.add("ddd");
150+
arrlist.add("dddd");
151+
arrlist.add("ddddd");
152+
System.out.println(arrlist); // [d, dd, ddd, dddd, ddddd]
153+
154+
Iterator<String> iterator = arrlist.iterator();
155+
while (iterator.hasNext())
156+
{
157+
String i = iterator.next();
158+
System.out.println(i);
159+
```
160+
161+
### `listIterator()`
162+
The listIterator () method of Java ArrayList returns a list iterator over the elements in this list starting at the specified position in this list.
163+
```
164+
ArrayList<String> arrlist = new ArrayList<String>();
165+
arrlist.add("d");
166+
arrlist.add("dd");
167+
arrlist.add("ddd");
168+
arrlist.add("dddd");
169+
arrlist.add("ddddd");
170+
System.out.println(arrlist); // [d, dd, ddd, dddd, ddddd]
171+
172+
ListIterator<String> iterator = arrlist.listIterator(2);
173+
while (iterator.hasNext())
174+
{
175+
String i = iterator.next();
176+
System.out.println(i);
177+
}
178+
```
179+
Output:
180+
```
181+
[d, dd, ddd, dddd, ddddd]
182+
ddd
183+
dddd
184+
ddddd
119185
```

0 commit comments

Comments
 (0)