Skip to content

Commit 1286808

Browse files
committed
Add arraylist java programs
1 parent f590fd2 commit 1286808

16 files changed

+522
-0
lines changed

collections/arraylist/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Java Collections - ArrayList in Java
2+
3+
This module contains articles related to ArrayList in Java.
4+
5+
## Related articles
6+
7+
- [ArrayList In Java](https://coderolls.com/arraylist-in-java/)
8+
- [How To Add An Element To ArrayList In Java?](https://coderolls.com/add-element-in-arraylist/)
9+
- [How To Change An Element In ArrayList?](https://coderolls.com/change-element-in-arraylist/)
10+
- [How To Remove An Element From An ArrayList?](https://coderolls.com/remove-element-from-arraylist/)
11+
- [Iterating the ArrayList In Java](https://coderolls.com/iterating-the-arraylist-in-java/)
12+
13+
14+
Visit [coderolls](https://coderolls.com/) to read more tutorials.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ArrayListAddExample;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* A Java program to add an element at the specific index.
7+
*
8+
* The add(int index, E element) method will add
9+
* an element at the specified index in ArrayList.
10+
*
11+
* @author Guarav Kukade at coderolls.com
12+
*
13+
*/
14+
public class ArrayListAddExample {
15+
16+
public static void main(String[] args) {
17+
18+
ArrayList<String> arrayList = new ArrayList<String>();
19+
20+
//adding elements to the arrayList using normal add method
21+
arrayList.add("Gaurav");
22+
arrayList.add("Shyam");
23+
24+
System.out.println(arrayList);// [Gaurav, Shyam]
25+
26+
// adding an element to the arrayList at index 1
27+
arrayList.add(1, "Saurav");
28+
29+
System.out.println(arrayList); // [Gaurav, Saurav, Shyam]
30+
31+
}
32+
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* A Java program to add an element to the list.
7+
*
8+
* The add(E e) method will add an element to
9+
* the end of the arrayList.
10+
*
11+
* @author Guarav Kukade at coderolls.com
12+
*
13+
*/
14+
public class ArrayListExample {
15+
16+
public static void main(String[] args) {
17+
18+
ArrayList<String> arrayList = new ArrayList<String>();
19+
20+
//adding elements to the arrayList
21+
arrayList.add("Gaurav");
22+
arrayList.add("Shyam");
23+
24+
System.out.println(arrayList);// [Gaurav, Shyam]
25+
26+
// adding an element to the arrayList
27+
// element will be added to the end of the arrayList
28+
arrayList.add("Saurav");
29+
30+
System.out.println(arrayList); // [Gaurav, Shyam, Saurav]
31+
32+
}
33+
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.gaurav.ExProject;
2+
3+
import java.util.ArrayList;
4+
5+
public class ArrayListDuplicate {
6+
7+
public static void main(String[] args) {
8+
ArrayList<String> arrayList = new ArrayList<String>();
9+
10+
arrayList.add("Gaurav");
11+
arrayList.add("Shyam");
12+
arrayList.add("Saurav");
13+
arrayList.add("Samiksha");
14+
arrayList.add("Rina");
15+
arrayList.add("Rina");
16+
17+
System.out.println(arrayList);
18+
19+
}
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.gaurav.ExProject;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* A Java program showing arraylist maintains the insertion order.
7+
*
8+
* @author gaurav
9+
*
10+
*/
11+
public class ArrayListExample {
12+
13+
public static void main(String[] args) {
14+
15+
ArrayList<String> arrayList = new ArrayList<String>();
16+
17+
arrayList.add("Gaurav");
18+
arrayList.add("Shyam");
19+
arrayList.add("Saurav");
20+
arrayList.add("Samiksha");
21+
arrayList.add("Rina");
22+
23+
System.out.println(arrayList);
24+
}
25+
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* A java program to change an element at
7+
* particular index in ArrayList.
8+
*
9+
* We can use 'public E set(int index, E element)' method
10+
* to change an element.
11+
*
12+
* @author Guarav Kukade at coderolls.com
13+
*
14+
*/
15+
public class ArrayListSetExample {
16+
17+
public static void main(String[] args) {
18+
ArrayList<String> arrayList = new ArrayList<String>();
19+
20+
//adding elements to the arrayList using normal add method
21+
arrayList.add("Gaurav");
22+
arrayList.add("Shyam");
23+
arrayList.add("Pradnya");
24+
25+
System.out.println(arrayList);// [Gaurav, Shyam, Pradnya]
26+
27+
// change an element at index 1
28+
arrayList.set(1, "Saurav");
29+
30+
System.out.println(arrayList); // [Gaurav, Saurav, Pradnya]
31+
32+
}
33+
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* A java program to iterate through arraylist
8+
* using the basic for loop.
9+
*
10+
* @author Gaurav Kukade at coderolls.com
11+
*
12+
*/
13+
public class ArrayListIterateExample1 {
14+
15+
public static void main (String[] args) {
16+
17+
List<String> list = new ArrayList<String>();
18+
19+
list.add("Monday");
20+
list.add("Tuesday");
21+
list.add("Wednesday");
22+
list.add("Thursday");
23+
list.add("Friday");
24+
list.add("Saturday");
25+
list.add("Sunday");
26+
27+
System.out.println("Iterating through ArrayList using the basic for loop.......\n");
28+
29+
for(int i =0; i <list.size(); i++){
30+
System.out.println("Object form the ArrayList at "+ i + " is " + list.get(i));
31+
}
32+
}
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* A java program to iterate through arraylist
8+
* using enhanced for loop i.e. For-each loop.
9+
*
10+
* @author Gaurav Kukade at coderolls.com
11+
*
12+
*/
13+
public class ArrayListIterateExample2 {
14+
15+
public static void main (String[] args) {
16+
17+
List<String> list = new ArrayList<String>();
18+
19+
list.add("Monday");
20+
list.add("Tuesday");
21+
list.add("Wednesday");
22+
list.add("Thursday");
23+
list.add("Friday");
24+
list.add("Saturday");
25+
list.add("Sunday");
26+
27+
System.out.println("Iterating through ArrayList using enhanced for loop i.e. For-each loop.......\n");
28+
29+
for(String str:list){
30+
//we get 'str' in for-each loop
31+
System.out.println("Object form the ArrayList is " + str);
32+
}
33+
}
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* A java program to iterate through arraylist
8+
* using while loop.
9+
*
10+
* @author Gaurav Kukade at coderolls.com
11+
*
12+
*/
13+
public class ArrayListIterateExample3 {
14+
15+
public static void main (String[] args) {
16+
17+
List<String> list = new ArrayList<String>();
18+
19+
list.add("Monday");
20+
list.add("Tuesday");
21+
list.add("Wednesday");
22+
list.add("Thursday");
23+
list.add("Friday");
24+
list.add("Saturday");
25+
list.add("Sunday");
26+
27+
System.out.println("Iterating through ArrayList using while loop.......\n");
28+
29+
int i=0;
30+
// while loop condition will be true till i is less the the list size
31+
while (list.size()>i) {
32+
System.out.println("Object form the ArrayList at "+ i + " is " + list.get(i));
33+
i++;
34+
}
35+
}
36+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
/**
8+
* A java program to iterate through arraylist
9+
* using Iterator interface.
10+
*
11+
* @author Gaurav Kukade at coderolls.com
12+
*
13+
*/
14+
public class ArrayListIterateExample4 {
15+
16+
public static void main (String[] args) {
17+
18+
List<String> list = new ArrayList<String>();
19+
20+
list.add("Monday");
21+
list.add("Tuesday");
22+
list.add("Wednesday");
23+
list.add("Thursday");
24+
list.add("Friday");
25+
list.add("Saturday");
26+
list.add("Sunday");
27+
28+
System.out.println("Iterating through ArrayList using Iterator interface........\n");
29+
30+
// getting iterator
31+
Iterator iterator = list.iterator();
32+
33+
// hasNext() returns true only if object is available at next call
34+
while (iterator.hasNext()) {
35+
36+
//next() returns obejct, we can cast it to reuqired type
37+
String str = (String) iterator.next();
38+
System.out.println("Object form the ArrayList is " + str);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy