-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListConverstion.java
More file actions
48 lines (47 loc) · 1.38 KB
/
Copy pathArrayListConverstion.java
File metadata and controls
48 lines (47 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Q3. Take an ArrayList of integers, convert it to a Vector, then sort the Vector in ascending order manually (without using Collections.sort).
What you practice:
ArrayList → Vector conversion
Manual sorting (bubble sort / selection sort)
Vector methods like add(), get(), set()
*/
import java.util.*;
public class ArrayListConverstion
{
public static void main(String x[])
{
ArrayList al = new ArrayList();
al.add(10);
al.add(20);
al.add(50);
al.add(40);
al.add(90);
al.add(80);
System.out.println("Array List elements are: "+al);
// converting array list into vector
Vector v = new Vector(al);
System.out.println("Vector Elements: "+v);
System.out.println("Accessing objects of the Vector using for loop");
for(int i = 0; i < al.size() - 1; i++)
{
for(int j = i+1; j < al.size(); j++)
{
Object obj = v.get(i); // acceessing i'th index value
int first = (int)obj; // converting objects into integer number
obj = v.get(j); // acceessing j'th index value
int last = (int)obj; // converting objects into integer number
if(first > last) // checking integer value
{
first = first + last;
last = first - last;
first = first - last;
}
// after swapping setting the values into there positions
v.set(i,first);
v.set(j,last);
}
}
// printing sorted vector
System.out.println("Vector after sorting: "+v);
}
}