Skip to content

Commit 20808f2

Browse files
authored
Add files via upload
1 parent eb779d9 commit 20808f2

File tree

6 files changed

+171
-0
lines changed

6 files changed

+171
-0
lines changed

Main_Array.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Main_Array {
2+
public static void main(String[] args) {
3+
//Arrays
4+
int[] array = {10, 20, 30, 40, 50};
5+
6+
System.out.println("Element at index 0:" + array[0]);
7+
System.out.println("Element at index 2:" + array[2]);
8+
System.out.println("Element at index 4:" + array[4]);
9+
10+
int sum = 0;
11+
for(int i = 0; i < array.length; i++){
12+
sum =+ array[i];
13+
}
14+
System.out.println("Sum of Array elements: " + sum);
15+
16+
array[2] = 35;
17+
System.out.println("Updated element at index 2: " + array[2]);
18+
19+
System.out.println("Element in the Array: " );
20+
21+
for(int i:array ){
22+
System.out.println(i);
23+
}
24+
}
25+
}

Main_ArrayList.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.*;
2+
3+
public class Main_ArrayList{
4+
5+
//Class method for the ArrayList.
6+
7+
8+
9+
public static void main(String[] args) {
10+
// Create an ArrayList to store integers
11+
ArrayList<Integer> arrayList = new ArrayList<Integer>(List.of(10, 20, 30, 40, 50));
12+
13+
// Output of elements with ref to their index.
14+
System.out.println("Element at index 0: " + arrayList.get(0));
15+
System.out.println("Element at index 2: " + arrayList.get(2));
16+
System.out.println("Element at index 4: " + arrayList.get(4));
17+
18+
//Sum of all elements in the arraylist
19+
int sum = arrayList.stream().mapToInt(Integer::intValue).sum();
20+
System.out.println("Sum of ArrayList elements: " + sum);
21+
22+
arrayList.set(2, 35);
23+
System.out.println("Updated element at index 2: " + arrayList.get(2));
24+
25+
System.out.println("Element in the ArrayList: ");
26+
for(int number: arrayList){
27+
System.out.println(number);
28+
}
29+
}
30+
}

Main_HashMap.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.*;
2+
3+
public class Main_HashMap {
4+
public static void main(String[] args) {
5+
HashMap <String, Integer> hashMap = new HashMap<>();
6+
//Adding keys to a hash map.
7+
hashMap.put("John", 25);
8+
hashMap.put("Alice", 30);
9+
hashMap.put("Bob", 35);
10+
11+
//Displaying the integer values.
12+
int alice = hashMap.get("Alice");
13+
int john = hashMap.get("John");
14+
System.out.println("Age of John: " + john);
15+
System.err.println("Age of Alice: " + alice);
16+
17+
//Checking if a key is present.
18+
Boolean isPresent = hashMap.containsKey("Bob");
19+
System.out.println("Is Bob present? " + isPresent);
20+
21+
//Printing all key-value pairs.
22+
for(String key: hashMap.keySet()){
23+
System.out.println(key+":"+hashMap.get(key));
24+
}
25+
26+
//Size of the hash map
27+
int Size = hashMap.size();
28+
System.out.println("Size of the hash map: "+Size);
29+
}
30+
}

Main_LinkedList.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.*;
2+
3+
public class Main_LinkedList {
4+
public static void main(String[] args) {
5+
//LinkedList.
6+
LinkedList <Integer> linkedList = new LinkedList<>();
7+
//Adding elements to linkedList
8+
linkedList.add(10);
9+
linkedList.add(20);
10+
linkedList.add(30);
11+
linkedList.add(40);
12+
linkedList.add(50);
13+
14+
System.out.println("LinkedList:"+ linkedList);
15+
16+
//Removing first element.
17+
linkedList.removeFirst();
18+
System.out.println("LinkedList after removing first element:"+linkedList);
19+
20+
//Checking if an element is in the linked list.
21+
Boolean Check = linkedList.contains(30);
22+
System.out.println("LinkedList contains element '30'? " + Check);
23+
24+
int first = linkedList.getFirst();
25+
int last = linkedList.getLast();
26+
27+
System.out.println("First element: " + first);
28+
System.out.println("Last element: " + last);
29+
30+
// Clearing the linked list elements
31+
linkedList.clear();
32+
System.out.println("LinkedList after clearing: " + linkedList);
33+
34+
}
35+
}

Main_Queue.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.*;
2+
3+
public class Main_Queue {
4+
public static void main(String[] args) {
5+
Queue <Integer> queue = new LinkedList<Integer>();
6+
queue.offer(10);
7+
queue.offer(20);
8+
queue.offer(30);
9+
queue.offer(40);
10+
queue.offer(50);
11+
12+
//Print first element
13+
int first = queue.peek();
14+
System.out.println("Front element: " + first);
15+
16+
//Dequeuing the elements.
17+
while(!queue.isEmpty()) {
18+
int e = queue.poll();
19+
System.out.println("Dequeue element: " + e);
20+
}
21+
}
22+
}

Main_Stack.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.*;
2+
3+
public class Main_Stack {
4+
public static void main(String[] args) {
5+
Stack<Integer> stack = new Stack<Integer>();
6+
7+
//Adding stack elements
8+
stack.push(10);
9+
stack.push(20);
10+
stack.push(30);
11+
12+
//Viewing the top elements of a STack.
13+
int top = stack.peek();
14+
System.out.println("Top element: " + top);
15+
System.out.println("Popped element: " + stack.pop());
16+
17+
//Checking if stack is empty.
18+
boolean isEmpty = stack.empty();
19+
System.out.println("Is stack empty: " + isEmpty);
20+
21+
//Checking the size
22+
int Size = stack.size();
23+
System.out.println("Stack size: " +Size);
24+
//Printing stack elements.
25+
System.out.println("Stack Elements:");
26+
for(int e : stack)
27+
System.out.println(e);
28+
}
29+
}

0 commit comments

Comments
 (0)