-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList Operations
More file actions
103 lines (78 loc) · 3.75 KB
/
ArrayList Operations
File metadata and controls
103 lines (78 loc) · 3.75 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.util.*;
class Main{
public static void main (String args[])
{
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<String> list2 = new ArrayList<String>(20);
ArrayList<Integer> list3 = new ArrayList<Integer>(list1);
list2.ensureCapacity(30); // increase the capacity of arraylist
list1.add(20);
list1.add(30);
list1.add(40);
list1.add(50);
System.out.println(list1); // to print total array
System.out.println(list1.isEmpty()); // check if list is empy or not
System.out.println(list1.contains(30)); // check the element is present or not
System.out.println(list1.indexOf(20)); // 0 index
System.out.println(list1.get(1)); //30 get perticular positon from array
System.out.println(list1.set(1,222)); // replace 30 with 22
list1.add(5,333); // add element at specific position , but only possible for last index plus 1
list1.remove(3); // remove element at specific positionm
list1.remove(30); // remove specific elemnt from the list
list1.trimToSize(); /// trim arraylist to existing size
Collections.reverse(list1); /// reverse the array list
//for connecting two array list
list1.addAll(list2);
// conver arrya to array list
String[] asset = {"equity", "stocks", "gold", "foreign exchange","fixed income", "futures", "options"};
List<String> assetList = Arrays.asList(asset);
//Converting list to an array
Object [] array = list1.toArray();
for(Object object : array)
{
System.out.println(object);
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////
Array List Iterations
package test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class ArrayListLoopExample {
public static void main(String args[]){
//Creating Arraylist for loop example
ArrayList<String> loopList = new ArrayList<String>();
//Storing elements in Java Arraylist
loopList.add("low cost personal loan");
loopList.add("cheap personal loan");
loopList.add("personal loan in 24 hours");
//Loop Arraylist using foreach loop of JDK1.5
System.out.println("=====================================================");
System.out.println("ArrayList Loop Example using foreach loop of JDK 1.5");
for(String element: loopList){
System.out.println(element);
}
//Loop Arraylist using simple for loop and size method
System.out.println("=====================================================");
System.out.println("ArrayList Loop Example using for loop and size()");
for(int i=0; i<loopList.size(); i++){
System.out.println(loopList.get(i));
}
//Iterate Arraylist using iterator and while loop in Java
System.out.println("=====================================================");
System.out.println("ArrayList Loop Example using Iterator and while loop");
Iterator<String> iterator = loopList.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
//Iterate Arraylist using ListIterator and while loop in Java
System.out.println("=====================================================");
System.out.println("ArrayList Loop Example using ListIterator and while loop");
ListIterator<String> listIterator = loopList.listIterator();
while(listIterator.hasNext()){
System.out.println(listIterator.next());
}
}
}