Skip to content

Commit a0493b9

Browse files
committed
Programs on ArrayList with proper comments for better understanding
1 parent 8c4c449 commit a0493b9

File tree

5 files changed

+104
-3
lines changed

5 files changed

+104
-3
lines changed

src/com/lab/dec_30/Test15.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.lab.dec_30;
2+
3+
import java.util.ArrayList;
4+
public class Test15
5+
{
6+
public static void main(String[] args)
7+
{
8+
// ArrayList of Stringer type
9+
ArrayList<String> list = new ArrayList<>();
10+
11+
/*
12+
* String type ArrayList can't hold Integer data
13+
list.add(12);
14+
list.add(16);
15+
list.add(34);
16+
list.add(78);
17+
*/
18+
19+
list.remove(Integer.valueOf(16));
20+
21+
System.out.println(list);
22+
}
23+
}
24+
25+
26+

src/com/lab/dec_31/Test11.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ public class Test11
77
{
88
public static void main(String[] argv)
99
{
10-
Collection c = new ArrayList();
10+
Collection c = new ArrayList(); // Arraylist object creation
1111
c.add("1");
1212
c.add("2");
1313
c.add("3");
1414

15-
Collection c1 = new ArrayList();
15+
Collection c1 = new ArrayList(); // Another Arraylist object creation
1616
c1.add("1");
17-
c.retainAll(c1);
17+
18+
// retainAll method will compare both c and c1 elements
19+
// and c will hold the elements that are smiler
20+
c.retainAll(c1);
21+
22+
// will print [1]
1823
System.out.println(c);
1924
}
2025
}

src/com/lab/dec_31/Test12.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.lab.dec_31;
2+
3+
import java.util.ArrayList;
4+
5+
public class Test12
6+
{
7+
public static void main(String[] args)
8+
{
9+
// String type ArrayList
10+
ArrayList<String> list = new ArrayList<String>();
11+
12+
list.add(null); // null
13+
list.add(0, "A"); // A null
14+
list.add(null); // A null null
15+
list.add(2, "B"); // A null B null
16+
list.add("20"); // A null B null 20
17+
list.add(1, "C"); // A C null B null 20
18+
19+
// will print [ A, C, null, B, null, 20 ]
20+
System.out.println(list);
21+
}
22+
}

src/com/lab/dec_31/Test13.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.lab.dec_31;
2+
3+
import java.util.ArrayList;
4+
public class Test13
5+
{
6+
public static void main(String[] args)
7+
{
8+
// ArrayList of String type
9+
ArrayList<String> list = new ArrayList<>();
10+
11+
// Can't hold Integer objects
12+
/*
13+
list.add(12);
14+
list.add(16);
15+
list.add(34);
16+
list.add(78);
17+
list.remove(12);
18+
*/
19+
20+
System.out.println(list);
21+
}
22+
}
23+

src/com/lab/dec_31/Test14.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.lab.dec_31;
2+
3+
import java.util.ArrayList;
4+
5+
public class Test14
6+
{
7+
public static void main(String[] args)
8+
{
9+
10+
// ArrayList of type String
11+
ArrayList<String> list = new ArrayList<>();
12+
13+
/*
14+
* String type ArrayList can't hold char type data
15+
list.add('a');
16+
list.add('b');
17+
list.add('c');
18+
list.add('d');
19+
list.remove('c');
20+
*/
21+
System.out.println(list);
22+
}
23+
}
24+
25+

0 commit comments

Comments
 (0)