Skip to content

Commit 3f27168

Browse files
committed
Add blogpost example for the arraylist in java
1 parent a75cdfa commit 3f27168

File tree

9 files changed

+348
-0
lines changed

9 files changed

+348
-0
lines changed
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.List;
5+
6+
/**
7+
* A Java program to add all elements of the specified collection
8+
* to the end of the ArrayList
9+
*
10+
* @author Gaurav Kukade at coderolls.com
11+
*
12+
*/
13+
public class ArrayListAddAllExample {
14+
15+
public static void main(String[] args) {
16+
17+
// ArrayList contains actresses
18+
List<String> actresses1 = new ArrayList<String>();
19+
20+
actresses1.add("Natalie Portman");
21+
actresses1.add("Abigail Anderson");
22+
actresses1.add("Jennifer Lawrence");
23+
24+
System.out.println("Actresses collection 1: \n"+ actresses1 );
25+
26+
// ArrayList contains actresses
27+
List<String> actresses2 = new ArrayList<String>();
28+
29+
30+
31+
actresses2.add("Angelina Jolie");
32+
actresses2.add("Scarlett Johansson");
33+
34+
System.out.println("Actresses collection 2: \n"+ actresses2);
35+
36+
// Add actresses2 collections all elements to actresses1 collection using addAll()method
37+
actresses1.addAll(actresses2);
38+
39+
System.out.println("Actresses : \n"+ actresses1);
40+
}
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* A Java program to add all elements of the specified collection
8+
* to the ArrayList from a particular index
9+
*
10+
* @author Gaurav Kukade at coderolls.com
11+
*
12+
*/
13+
public class ArrayListAddAllExample2 {
14+
15+
public static void main(String[] args) {
16+
17+
// ArrayList containing entrepreneurs
18+
List<String> entrepreneurs1 = new ArrayList<String>();
19+
20+
entrepreneurs1.add("Bill Gates");
21+
entrepreneurs1.add("Steve Jobs");
22+
entrepreneurs1.add("Jeff Bezos");
23+
entrepreneurs1.add("Richard Branson");
24+
entrepreneurs1.add("Mark Cuban");
25+
26+
System.out.println("Entrepreneurs collection 1: \n"+ entrepreneurs1 );
27+
28+
// ArrayList containing entrepreneurs
29+
List<String> entrepreneurs2 = new ArrayList<String>();
30+
31+
entrepreneurs2.add("Mark Zuckerberg");
32+
entrepreneurs2.add("Larry Page");
33+
entrepreneurs2.add("Larry Ellison");
34+
35+
System.out.println("Entrepreneurs collection 2: \n"+ entrepreneurs2);
36+
37+
// Add entrepreneurs2 collections all elements to entrepreneurs1 collection
38+
// starting from index 2
39+
entrepreneurs1.addAll(2, entrepreneurs2);
40+
41+
System.out.println("Entrepreneurs : \n"+ entrepreneurs1);
42+
}
43+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* A java program to explain the indexOf() method
8+
* of the arrayList class in java.
9+
*
10+
* @author Gaurav Kukade
11+
*/
12+
public class ArrayListIndexOfExample {
13+
14+
public static void main(String[] args) {
15+
16+
// create an empty arraylist object 'states'
17+
List<String> states = new ArrayList<String>();
18+
19+
// add state in the arraylist, Florida multiple times
20+
states.add("California");
21+
states.add("Texas");
22+
states.add("Florida");
23+
states.add("Florida");
24+
states.add("New Jersey");
25+
states.add("Washington");
26+
states.add("Florida");
27+
28+
int index = states.indexOf("Florida");
29+
30+
//prints index of the first occurrences of Florida i.e. 2
31+
System.out.println("The index of the Florida: "+ index);
32+
33+
//trying get the index of element, which is not present
34+
int index2 = states.indexOf("Alaska");
35+
36+
System.out.println("The index of the Alaska: " + index2);
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* A java program to explain the indexOf() method
8+
* of the arrayList class in java.
9+
*
10+
* @author Gaurav Kukade
11+
*/
12+
public class ArrayListLastIndexOfExample {
13+
14+
public static void main(String[] args) {
15+
16+
// create an empty arraylist object 'states'
17+
List<String> states = new ArrayList<String>();
18+
19+
// add state in the arraylist, Florida multiple times
20+
states.add("California");
21+
states.add("Texas");
22+
states.add("Florida");
23+
states.add("Florida");
24+
states.add("New Jersey");
25+
states.add("Washington");
26+
states.add("Florida");
27+
28+
int index = states.lastIndexOf("Florida");
29+
30+
//prints index of the last occurrences of Florida i.e. 6
31+
System.out.println("The last index of the Florida: "+ index);
32+
33+
//trying get the index of element, which is not present
34+
int index2 = states.lastIndexOf("Alaska");
35+
36+
System.out.println("The index of the Alaska: " + index2);
37+
}
38+
39+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* A java program to to eplain the retainAll() method of the Arraylist class.
8+
*
9+
* @author Gaurav Kukade at coderolls.com
10+
*
11+
*/
12+
public class ArrayListRetainAllExample {
13+
14+
public static void main(String[] args) {
15+
16+
//create an empty arraylist of Integer to retain elements
17+
List<Integer> numbers1 = new ArrayList<Integer>();
18+
19+
//add number to the list
20+
numbers1.add(4);
21+
numbers1.add(5);
22+
numbers1.add(6);
23+
24+
25+
//create an empty arraylist of Integer
26+
List<Integer> numbers2 = new ArrayList<Integer>();
27+
28+
// add number to the list
29+
numbers2.add(1);
30+
numbers2.add(2);
31+
numbers2.add(3);
32+
numbers2.add(4);
33+
numbers2.add(5);
34+
numbers2.add(6);
35+
numbers2.add(7);
36+
numbers2.add(8);
37+
numbers2.add(9);
38+
39+
40+
System.out.println("Lists before the method call");
41+
System.out.println("numbers1: "+ numbers1);
42+
System.out.println("numbers2: "+ numbers2);
43+
44+
//retain all the elements of numbers1 in the list numbers2
45+
numbers2.retainAll(numbers1);
46+
47+
System.out.println("\nLists after the method call");
48+
System.out.println("numbers1: "+ numbers1);
49+
System.out.println("numbers2: "+ numbers2);
50+
}
51+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
/**
6+
* A java program to show the NullPointerException in retainAll() method
7+
* of the arrayList class.
8+
*
9+
* @author gaurav
10+
*
11+
*/
12+
public class RetainAllNullPointerExceptionExample {
13+
14+
public static void main(String[] args) {
15+
16+
//create an empty arraylist of Integer to retain elements
17+
List<String> numbers1 = null;
18+
19+
//create an empty arraylist of Integer
20+
List<Integer> numbers2 = new ArrayList<Integer>();
21+
22+
// add number to the list
23+
numbers2.add(1);
24+
numbers2.add(2);
25+
numbers2.add(3);
26+
numbers2.add(4);
27+
28+
System.out.println("Lists before the method call");
29+
System.out.println("numbers1: "+ numbers1);
30+
System.out.println("numbers2: "+ numbers2);
31+
32+
//retain all the elements of numbers1 in the list numbers2
33+
numbers2.retainAll(numbers1);
34+
35+
System.out.println("\nLists after the method call");
36+
System.out.println("numbers1: "+ numbers1);
37+
System.out.println("numbers2: "+ numbers2);
38+
}
39+
}
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 explain the subList() method of the ArrayList class in Java
8+
*
9+
* @author Gaurav Kukade at coderolls.com
10+
*
11+
*/
12+
public class ArrayListSubListExample {
13+
14+
public static void main(String[] args) {
15+
16+
// create an empty arraylist object 'states'
17+
List<String> states = new ArrayList<String>();
18+
19+
// add state in the arraylist
20+
states.add("California");
21+
states.add("Texas");
22+
states.add("Florida");
23+
states.add("New Jersey");
24+
states.add("Washington");
25+
26+
System.out.println("The states list: \n"+ states);
27+
28+
// sublist the states list from 1 to 3 index
29+
List<String> statesSubList = states.subList(1, 3);
30+
31+
System.out.println("\nThe states sublist from index 1 (inclusive) to 3 (exclusive): \n"+ statesSubList);
32+
}
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* A java program to explain the subList() methods
8+
* IndexOutOfBoundsException exception case.
9+
*
10+
* @author Gaurav Kukade at coderolls.com
11+
*/
12+
public class SubListMethodExceptionExample1 {
13+
14+
public static void main(String[] args) {
15+
// create an empty arraylist object 'states'
16+
List<String> states = new ArrayList<String>();
17+
18+
// add state in the arraylist
19+
states.add("California");
20+
states.add("Texas");
21+
states.add("Florida");
22+
states.add("New Jersey");
23+
states.add("Washington");
24+
25+
System.out.println("The states list: \n"+ states);
26+
27+
// will throw IndexOutOfBoundsException
28+
List<String> statesSubList = states.subList(1, 7);
29+
30+
System.out.println("\nThe states sublist from index 1 (inclusive) to 3 (exclusive): \n"+ statesSubList);
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* A java program to explain the subList() methods
8+
* IllegalArgumentException exception case.
9+
*
10+
* @author Gaurav Kukade at coderolls.com
11+
*/
12+
public class SubListMethodExceptionExample2 {
13+
14+
public static void main(String[] args) {
15+
// create an empty arraylist object 'states'
16+
List<String> states = new ArrayList<String>();
17+
18+
// add state in the arraylist
19+
states.add("California");
20+
states.add("Texas");
21+
states.add("Florida");
22+
states.add("New Jersey");
23+
states.add("Washington");
24+
25+
System.out.println("The states list: \n"+ states);
26+
27+
// will throw IllegalArgumentException
28+
List<String> statesSubList = states.subList(3, 1);
29+
30+
System.out.println("\nThe states sublist from index 1 (inclusive) to 3 (exclusive): \n"+ statesSubList);
31+
}
32+
}

0 commit comments

Comments
 (0)