Skip to content

Commit 5b72fbe

Browse files
committed
Programs on Vector and ArrayList with proper comments for better understanding
1 parent 316019e commit 5b72fbe

14 files changed

+351
-0
lines changed

src/com/lab/dec_30/Test1.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.lab.dec_30;
2+
3+
import java.util.*;
4+
class Test1
5+
{
6+
public static void main(String args[])
7+
{
8+
// here we are creating vector class object
9+
// constructor is taking [ int initialCapacity, int capacityIncrement ]
10+
Vector obj = new Vector(4,2);
11+
12+
// This type of constructor is removed [ Deprecated ] from integer class since JAVA-9
13+
obj.addElement(new Integer(3));
14+
obj.addElement(new Integer(2));
15+
obj.addElement(new Integer(5));
16+
17+
// This line will print element which is at 1 index [ 2 ]
18+
// Vector is internally array thats why it adds elements as per indexes
19+
System.out.println(obj.elementAt(1));
20+
}
21+
}

src/com/lab/dec_30/Test10.java

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

src/com/lab/dec_30/Test11.java

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

src/com/lab/dec_30/Test12.java

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

src/com/lab/dec_30/Test13.java

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

src/com/lab/dec_30/Test14.java

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

src/com/lab/dec_30/Test2.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.lab.dec_30;
2+
3+
import java.util.*;
4+
5+
public class Test2
6+
{
7+
public static void main(String args[])
8+
{
9+
//This is calling constructor Vector(int initialCapacity, int capacityIncrement)
10+
Vector obj = new Vector(4,2);
11+
// InitialCapacity = 4
12+
// CapacityIncreament = 2
13+
14+
// Here we are adding elements to vector
15+
obj.addElement(new Integer(3));
16+
obj.addElement(new Integer(2));
17+
obj.addElement(new Integer(5));
18+
19+
// Capacity will be printed [ 4 ]
20+
System.out.println(obj.capacity());
21+
}
22+
}

src/com/lab/dec_30/Test3.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.lab.dec_30;
2+
import java.util.*;
3+
4+
public class Test3
5+
{
6+
public static void main(String args[])
7+
{
8+
Vector obj = new Vector(4,2);
9+
10+
obj.addElement(new Integer(3));
11+
obj.addElement(new Integer(2));
12+
obj.addElement(new Integer(6));
13+
14+
// Here we are inserting element 8 at position 2
15+
obj.insertElementAt(new Integer(8), 2);
16+
17+
// This will print toString method of Vector class
18+
// [ 3, 2, 8, 6 ]
19+
System.out.println(obj);
20+
}
21+
22+
}

src/com/lab/dec_30/Test4.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.lab.dec_30;
2+
import java.util.*;
3+
4+
public class Test4
5+
{
6+
public static void main(String args[])
7+
{
8+
Vector obj = new Vector(4,2);
9+
10+
obj.addElement(new Integer(3));
11+
obj.addElement(new Integer(2));
12+
obj.addElement(new Integer(5));
13+
14+
// here remove all will remove all elements from vector
15+
obj.removeAll(obj);
16+
17+
// isEmpty method will check weather the vector is empty of not
18+
// true
19+
System.out.println(obj.isEmpty());
20+
21+
// will print empty array
22+
// [ ]
23+
System.out.println(obj);
24+
}
25+
}
26+
27+
28+

src/com/lab/dec_30/Test5.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.lab.dec_30;
2+
3+
import java.util.*;
4+
public class Test5
5+
{
6+
public static void main(String args[])
7+
{
8+
// Array of lenght 5 will be created
9+
int array[] = new int [5];
10+
11+
12+
// array will be filled with 5-1 values
13+
// [ 5, 4, 3, 2, 1 ]
14+
for (int i = 5; i > 0; i--)
15+
array[5-i] = i;
16+
17+
18+
/*
19+
public static void fill(int[] a, int fromIndex, int toIndex, int val)
20+
{
21+
rangeCheck(a.length, fromIndex, toIndex);
22+
for (int i = fromIndex; i < toIndex; i++)
23+
a[i] = val;
24+
}
25+
*/
26+
27+
// This fill method is taking parameters
28+
// 1. Array[] => array
29+
// 2. fromIndex => 1
30+
// 3. toIndex => 4
31+
// 4. value => 8
32+
Arrays.fill(array, 1, 4, 8);
33+
// The above method will fill array with value 8
34+
// from index 1 to index 4 that is [1], [2], [3]
35+
// array [ 5, 8, 8, 8, 1 ]
36+
37+
38+
for (int i = 0; i < 5 ; i++)
39+
System.out.print(array[i]+" ");
40+
}
41+
}

src/com/lab/dec_30/Test6.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.lab.dec_30;
2+
3+
import java.util.*;
4+
public class Test6
5+
{
6+
public static void main(String args[])
7+
{
8+
// Creating objects for ArrayList
9+
ArrayList obj1 = new ArrayList();
10+
ArrayList obj2 = new ArrayList();
11+
12+
// Adding objects to ArrayList
13+
obj1.add("A");
14+
obj1.add("B");
15+
obj2.add("A");
16+
17+
// Adding "B" at index 1
18+
obj2.add(1, "B");
19+
20+
// Will check that the obj1 is equals to obj2 or not
21+
// True
22+
System.out.println(obj1.equals(obj2));
23+
}
24+
}
25+
26+
27+

src/com/lab/dec_30/Test7.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.lab.dec_30;
2+
import java.util.ArrayList;
3+
import java.util.List;
4+
5+
public class Test7
6+
{
7+
public static void main(String[] args)
8+
{
9+
List<String> list = new ArrayList<String>();
10+
11+
list.add("Patna"); // ["Patna"]
12+
list.add(0, "New York"); // ["New York", "Patna"]
13+
list.add("Mumbai"); // ["New York", "Patna", "Mumbai"]
14+
list.add(2, "Sydney"); // ["New York", "Patna", "Sydney, "Mumbai"]
15+
16+
//This line will print the list
17+
// ["New York", "Patna", "Sydney, "Mumbai"]
18+
System.out.println(list);
19+
}
20+
}
21+

src/com/lab/dec_30/Test8.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.lab.dec_30;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Test8
7+
{
8+
public static void main(String[] args)
9+
{
10+
// Creating ArrayList Object
11+
List<String> list = new ArrayList<String>();
12+
13+
14+
list.add("Orange"); // list ["Orange"]
15+
list.add(0, "Banana"); // list ["Banana", "Orange"]
16+
17+
//New ArrayList object created i.e arList
18+
ArrayList<String> arList = new ArrayList<>();
19+
20+
arList.add("Apple"); // arList ["Apple"]
21+
list.add("Grapes"); // list ["Banana", "Orange", "Grapes"]
22+
23+
// All elements of arList will be append to list
24+
// list ["Banana", "Orange", "Grapes"] + arList ["Apple"]
25+
list.addAll(3, arList); // list ["Banana", "Orange", "Grapes" ,"Apple"]
26+
27+
// ["Banana", "Orange", "Grapes" ,"Apple"]
28+
System.out.println(list);
29+
}
30+
}
31+

src/com/lab/dec_30/Test9.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.lab.dec_30;
2+
3+
import java.util.ArrayList;
4+
public class Test9
5+
{
6+
public static void main(String[] args)
7+
{
8+
ArrayList<String> list = new ArrayList<String>();
9+
10+
// null added to 0th index
11+
list.add(null); // [null]
12+
13+
list.add(0, "A"); // [null, "A"]
14+
15+
// Exception will occur
16+
// i.e [ java.lang.IndexOutOfBoundsException ]
17+
list.add(3, "B"); // We can not insert element at 3 without inserting at 2
18+
list.add(1, "C");
19+
20+
System.out.println(list);
21+
}
22+
}

0 commit comments

Comments
 (0)